2013年9月28日星期六

Java way to post a request on the https protocol site issues

Request online to find a variety of ways https protocol , almost all the following forms:


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;
}


}


However, I used this method to access it , or not being given :
[16:38:19.303] java.lang.IllegalArgumentException: protocol = https host = null

Which god , there is wood there a good way ah.


------ Solution ------------------------------------ --------
try SSLContext sc = SSLContext.getInstance ("SSL", "SunJSSE");
------ For reference only - -------------------------------------
or not , the same error
- ----- For reference only ---------------------------------------
which line newspaper 's fault ?
------ For reference only -------------------------------------- -
reported that this line conn.connect ();
------ For reference only --------------------- ------------------
this is the wrong message
------ For reference only ----- ----------------------------------
commented conn.setHostnameVerifier (new TrustAnyHostnameVerifier ()); try
------ For reference only ------------------------------------ ---
commented , still the same error what method you use to access the https ?
------ For reference only -------------------------------------- -
requested address was wrong , https ; less behind both slash. . . Plus after it. . . Own imprudent . .

没有评论:

发表评论