1、请求HTTPS的站点
private static void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new 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[0];
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
//this is important
trustEveryone();
Connection.Response response = Jsoup.connect(url)
.method(Connection.Method.GET)
.headers(headerMap)
.ignoreContentType(true)
.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
2、实现带Payload的请求
public static void main(String[] args) {
try {
Connection.Response response = Jsoup.connect(url)
.method(Connection.Method.GET)
.headers(headerMap)
//this is important
.requestBody(JSON.toJSONString(param))
//this is important
.ignoreContentType(true)
.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
3、HTTP error fetching URL
org.jsoup.HttpStatusException: HTTP error fetching URL
查看HttpConnection.execute()
源码
if ((status < 200 || status >= 400) && !req.ignoreHttpErrors()) {
throw new HttpStatusException("HTTP error fetching URL", status, req.url().toString());
}
该默认处理方式,不利于定位问题,调用ignoreHttpErrors
方法可以查看返回的错误码
Connection.Response response = Jsoup.connect(url)
//this is important
.ignoreHttpErrors(false)
.execute();