大家好,欢迎来到IT知识分享网。
MyX509TrustManager.java
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
public class MyX509TrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
TrustAnyHostnameVerifier.java
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
public class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
// 直接返回true
return true;
}
}
test.java
public static String gethtml() throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
//解决Javajdk1.6,https的问题
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlx = new URL("https://abc.com/?sss=3434343");
//HttpURLConnection uc = (HttpURLConnection) urlx.openConnection();
HttpsURLConnection uc = (HttpsURLConnection) urlx.openConnection();
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestMethod("GET");
uc.setInstanceFollowRedirects(false);
uc.setConnectTimeout(10 * 1000);
uc.setReadTimeout(10*1000);
uc.setSSLSocketFactory(ssf);//解决Javajdk1.6,https的问题
uc.setHostnameVerifier(new TrustAnyHostnameVerifier());//解决Javajdk1.6,https的问题
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty(
"Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36");
uc.setRequestProperty("Cookie", "");
String vhtml = "";
System.out.println(uc.getResponseCode());
String htmltype = "utf-8";
try {
java.io.InputStream inputstream = uc.getInputStream();
BufferedReader bufferedreader = null;
if (htmltype == null || htmltype.trim().equals(""))
bufferedreader = new BufferedReader(new InputStreamReader(
inputstream));
else
bufferedreader = new BufferedReader(new InputStreamReader(
inputstream, htmltype));
String s1;
// String vhtml = "";
while ((s1 = bufferedreader.readLine()) != null) {
vhtml = vhtml + s1;
}
return vhtml;
} catch (Exception e) {
// TODO: handle exception
}
return vhtml;
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/26355.html