Java发送http请求出现Server sent an unsupported extension: type_35

下辈子不做程序猿 2017-11-13 04:47:44
下面这个方法,之前在eclipse上用一直没问题,后面不知道做了什么,这个方法就一直报错

但是我把项目放到服务器上去,就没问题,放到我同事的电脑上也没问题,所以我觉得应该是我的电脑缺少了什么东西。
有没有谁遇到过这样的情况,我开发的时候很苦恼,我现在得到的有可能解决的方法是重装系统,但是我并不想重装啊!!!
谁能帮忙解决一下,万分感激啊,这是第一次发帖子!

/**
* 用form表单形式post访问接口,返回json
* @param url
* @param params
* @return response.toString()
*/
public static String formPost(String url,String params){
String responseMessage = "";
StringBuffer response = new StringBuffer();
HttpURLConnection httpConnection = null;
OutputStreamWriter out = null;
BufferedReader reader = null;
try {
URL urlPost = new URL(url);
httpConnection = (HttpURLConnection) urlPost.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
// 参数长度太大,不能用get方式
httpConnection.setRequestMethod("POST");
// 不使用缓存
httpConnection.setUseCaches(false);
// URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
httpConnection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数
httpConnection.setRequestProperty("Connection", "Keep-Alive");
// 设置请求头信息
httpConnection.setRequestProperty("Charset", "UTF-8");
// 设置边界
//httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
// 实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。

httpConnection.connect();
out = new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8");
// 构建请求参数
StringBuffer sb = new StringBuffer();
sb.append(params);
out.write(sb.toString());
System.out.println("send_url:" + url);
System.out.println("send_data:" + sb.toString());
// flush and close
out.flush();

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.close();
}
if (null != reader) {
reader.close();
}
if (null != httpConnection) {
httpConnection.disconnect();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

try {
reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"UTF-8"));
while ((responseMessage = reader.readLine()) != null) {
response.append(responseMessage);
response.append("\n");
}

if (!"failure".equals(response.toString())) {
System.out.println("post链接success");
} else {
System.out.println("post链接failue");
}
// 将该url的配置信息缓存起来
return response.toString();
} catch (IOException e) {
e.printStackTrace();
return response.toString();
}
}
...全文
528 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 12 楼 l359122505 的回复:
你们服务器外层有没有购买防火墙
这个我倒是不清楚啊,服务器上 这些代码能用,我同事的电脑这些代码也能用,就是我电脑上不知道是不是缺少了什么东西。只是这个代码在我电脑上才抛这个异常。
繁华终归落尽 2017-11-15
  • 打赏
  • 举报
回复
你们服务器外层有没有购买防火墙
  • 打赏
  • 举报
回复
引用 4 楼 l359122505 的回复:
访问的是https请求吗?
是的 https会出这样的问题,http不会
引用 10 楼 l359122505 的回复:

private static final SSLHandler simpleVerifier = new SSLHandler();
private static SSLSocketFactory sslFactory;

public static void formPost(String url,String params){
    ......
    httpConnection.setSSLSocketFactory(getSSLSF());
    httpConnection.setHostnameVerifier(simpleVerifier);
    ......
}
public static synchronized SSLSocketFactory getSSLSF() throws Exception
	{
		if(sslFactory!=null) return sslFactory; 
		SSLContext sc = SSLContext.getInstance("SSLv3");
		sc.init(null, new TrustManager[]{simpleVerifier}, null);
		sslFactory = sc.getSocketFactory();
		return sslFactory;
	}
    
    private static class SSLHandler implements X509TrustManager,HostnameVerifier
	{	
		private SSLHandler()
		{
		}
		
		public void checkClientTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean verify(String arg0, SSLSession arg1)
		{
			return true;
		}
	}
谢谢你的回复,我这里之前有写过这种方法,现在也是不能用。

public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
			
			JSONObject jsonObject = null;
			
			try {
				// 创建SSLContext对象,并使用我们指定的信任管理器初始化
				TrustManager[] tm = { new HttpsX509TrustManager() };
				SSLContext sslContext = SSLContext.getInstance("SSLv3", "SunJSSE");
				sslContext.init(null, tm, new java.security.SecureRandom());
				// 从上述SSLContext对象中得到SSLSocketFactory对象
				SSLSocketFactory ssf = sslContext.getSocketFactory();

				URL url = new URL(requestUrl);
				HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
				conn.setSSLSocketFactory(ssf);
				
				conn.setDoOutput(true);
				conn.setDoInput(true);
				conn.setUseCaches(false);
				// 设置请求方式(GET/POST)
				conn.setRequestMethod(requestMethod);

				// 当outputStr不为null时向输出流写数据
				if (null != outputStr) {
					OutputStream outputStream = conn.getOutputStream();
					// 注意编码格式
					outputStream.write(outputStr.getBytes("UTF-8"));
					outputStream.close();
				}

				// 从输入流读取返回内容
				InputStream inputStream = conn.getInputStream();
				InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
				String str = null;
				StringBuffer buffer = new StringBuffer();
				while ((str = bufferedReader.readLine()) != null) {
					buffer.append(str);
				}

				// 释放资源
				bufferedReader.close();
				inputStreamReader.close();
				inputStream.close();
				inputStream = null;
				conn.disconnect();
				jsonObject = JSONObject.fromObject(buffer.toString());
			} catch (ConnectException ce) {
				ce.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonObject;
		}
繁华终归落尽 2017-11-15
  • 打赏
  • 举报
回复

private static final SSLHandler simpleVerifier = new SSLHandler();
private static SSLSocketFactory sslFactory;

public static void formPost(String url,String params){
    ......
    httpConnection.setSSLSocketFactory(getSSLSF());
    httpConnection.setHostnameVerifier(simpleVerifier);
    ......
}
public static synchronized SSLSocketFactory getSSLSF() throws Exception
	{
		if(sslFactory!=null) return sslFactory; 
		SSLContext sc = SSLContext.getInstance("SSLv3");
		sc.init(null, new TrustManager[]{simpleVerifier}, null);
		sslFactory = sc.getSocketFactory();
		return sslFactory;
	}
    
    private static class SSLHandler implements X509TrustManager,HostnameVerifier
	{	
		private SSLHandler()
		{
		}
		
		public void checkClientTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean verify(String arg0, SSLSession arg1)
		{
			return true;
		}
	}
繁华终归落尽 2017-11-15
  • 打赏
  • 举报
回复
回复不了私信,网上有示例HttpClient如何绕过证书验证,你查一下相关资料
suixixiang8140 2017-11-15
  • 打赏
  • 举报
回复
这个问题有没有人知道
110成成 2017-11-14
  • 打赏
  • 举报
回复
93行是哪一句。。
  • 打赏
  • 举报
回复
引用 3 楼 tianfang 的回复:
加密算法协商中出的问题 貌似在jdk6中才有,升级jdk试试
我已经是jdk1.8 了,我晚上重装一下试试
  • 打赏
  • 举报
回复
引用 2 楼 zc881124 的回复:
93行是哪一句。。
httpConnection.connect(); 这一句,贴出来的代码里面 的36行
  • 打赏
  • 举报
回复
引用 1 楼 tangyanzhi1111 的回复:
你把jdk重新装一遍不就行了?干嘛要重装系统
这个问题重装jdk就能解决了吗?那我晚上回去试试
繁华终归落尽 2017-11-14
  • 打赏
  • 举报
回复
访问的是https请求吗?
tianfang 2017-11-14
  • 打赏
  • 举报
回复
加密算法协商中出的问题 貌似在jdk6中才有,升级jdk试试
江湖评谈 2017-11-13
  • 打赏
  • 举报
回复
你把jdk重新装一遍不就行了?干嘛要重装系统

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧