Jsoup常见使用问题

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();

猜你喜欢

转载自blog.csdn.net/weixin_42142408/article/details/80856405