/**
* 過濾https協議
* @author edwin
*/
public class FiltratHttpsUtils {
static HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName + " vs. "+ session.getPeerHost());
return true;
}
};
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
public static void doFiltra() throws Exception {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
只需要在請求前調用:FiltratHttpsUtils .doFiltra()即可。
package com.dao;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.dao.HttpsUrlValidator.FiltratHttpsUtils;
public class PhotoVerification {
public static void main(String[] args) throws Exception {
try {
FiltratHttpsUtils .doFiltra();
String url="https://inv-veri.chinatax.gov.cn/index.html";
// HttpsUrlValidator.retrieveResponseFromServer(url);
// doc = Jsoup.connect(url).header("User-Agent",rand_agents).timeout(10000).get();
// body = doc.getElementsByTag("body").html();
// log.info(e.getMessage());
//
//通過Jsoup的Connect方法獲取document類
Document document = Jsoup.connect(url).get();
// System.out.println(document.title());//控制台打印網頁標題
//根據class獲取Elements類
// Element timeElement = document.getElementById("yzminfo");//文字
// Element titleElement = document.getElementById("yzm_img");//圖片的base64碼
//
Element timeElement = document.getElementById("yzminfo");//文字
Element titleElement = document.getElementById("imgarea");//圖片的base64碼
System.out.println(timeElement);
System.out.println("----------------------");
System.out.println(titleElement);
//指定文件名及路徑
// File file = new File("D:\\title.txt");
// File contentFile = new File("D:\\content.txt");
// if(!file.exists()){
// file.createNewFile();
// }
// if(!contentFile.exists()){
// contentFile.createNewFile();
// }
// //寫入本地
// PrintWriter pw = new PrintWriter("D:\\title.txt");
// PrintWriter contentPw = new PrintWriter("D:\\content.txt");
// pw.close();
//
//
//// String href = titleElement.get(i).attr("href");//取出新聞標題的url
// String schoolHref = "http://www.haie.edu.cn/";
//因為取出來的新聞url不規范,直接訪問不了,需要將其拼接成正常的網頁url
// String contentHref = schoolHref+href;
//重復第一步的內容,根據URL取Documet類
// Document contentDoc = Jsoup.connect(contentHref).get();
//繼續觀察網頁,取出新聞詳細頁面的文字。
// Elements contentElement = contentDoc.getElementsByClass("contentstyle125127");
// Elements authorElement = contentDoc.getElementsByClass("authorstyle125127");
// String content = contentElement.text();
// String author = authorElement.text();
//打印出作者,新聞詳細內容
// contentPw.println(author);
// contentPw.println(content);
// contentPw.println("---------------------------");
//
// contentPw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
