如何使用java读取https页面内容?

时间:2010-07-01 06:22:16

标签: java

如何使用java读取https页面内容??

1 个答案:

答案 0 :(得分:0)

以下是检索“ https://maven.apache.org/guides/mini/guide-repository-ssl.html”页面内容的示例,仅在不需要登录的情况下,它适用于所有具有https协议的页面。只需替换httpsURL字符串即可。享受吧!

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

/**
 * <b>Get the Html source from a https url </b>
 */
public class HttpsClientUtil {
    public static void main(String[] args) throws Exception {
        String httpsURL = "https://maven.apache.org/guides/mini/guide-repository-ssl.html";

        URL myurl = new URL(httpsURL);
        HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
        con.setRequestProperty ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" );
        InputStream ins = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader in = new BufferedReader(isr);
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }
        in.close();
    }
}