关于java以post方式请求https协议网站问题

码个蛋蛋 2013-09-27 07:10:45
网上找了很多种请求https协议的方法,差不多都是以下的形式:


public class HttpsUtil{

private static class TrustAnyTrustManager implements X509TrustManager {

public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}

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

private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}


/**
* post方式请求服务器(https协议)
* @param url
* 请求地址
* @param content
* 参数
* @param charset
* 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public static byte[] post(String url, String content, String charset)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
new java.security.SecureRandom());

URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(content.getBytes(charset));
// 刷新、关闭
out.flush();
out.close();
InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
return outStream.toByteArray();
}
return null;
}


}


但是,我用这种方法访问的时候,还是不行会报错:
[16:38:19.303] java.lang.IllegalArgumentException: protocol = https host = null

哪位大神,有木有好的方法啊。

...全文
6103 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
u014494398 2015-10-22
  • 打赏
  • 举报
回复
这个代码写完以后怎么进行测试啊
终结者aihua 2015-01-04
  • 打赏
  • 举报
回复
你好,我在做https请求的时候用了一下阁下的代码,在代码中报错,错误是:cannot write to a URLConnection if doOutput=false - call setDoOutput(true)。然后我加上了以下两句代码就可以了, // 发送POST请求设置如下两行 conn.setDoOutput(true); conn.setDoInput(true);
码个蛋蛋 2013-09-28
  • 打赏
  • 举报
回复
请求的地址错了,https;后面少了俩斜杠。。。加上后就可以了。。。自己太大意了。。
码个蛋蛋 2013-09-28
  • 打赏
  • 举报
回复
引用 6 楼 fudongrifdr 的回复:
注释掉conn.setHostnameVerifier(new TrustAnyHostnameVerifier());试试
注释掉,还是一样的错误你是用什么方法访问https的?
末日哥 2013-09-28
  • 打赏
  • 举报
回复
注释掉conn.setHostnameVerifier(new TrustAnyHostnameVerifier());试试
码个蛋蛋 2013-09-28
  • 打赏
  • 举报
回复
引用 3 楼 fudongrifdr 的回复:
哪一行报的错?
这是错误信息
码个蛋蛋 2013-09-28
  • 打赏
  • 举报
回复
引用 3 楼 fudongrifdr 的回复:
哪一行报的错?
报的是这一行 conn.connect();
末日哥 2013-09-28
  • 打赏
  • 举报
回复
哪一行报的错?
码个蛋蛋 2013-09-28
  • 打赏
  • 举报
回复
引用 1 楼 fudongrifdr 的回复:
试试SSLContext sc= SSLContext.getInstance("SSL", "SunJSSE");
还是不行,同样的错误
末日哥 2013-09-27
  • 打赏
  • 举报
回复
试试SSLContext sc= SSLContext.getInstance("SSL", "SunJSSE");

67,513

社区成员

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

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