Want to improve this question? Update the question so it focuses on one problem only by editing this post.
开发者_JS百科Closed 4 years ago.
Improve this questionHow to read the https page content using java??
Following is an example of retrieving the content of the page for "https://maven.apache.org/guides/mini/guide-repository-ssl.html" It works for any page that have https protocol only if a login is NOT required. Just replace the httpsURL string. Enjoy!
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();
}
}
精彩评论