天天看點

java 抓取 https網頁_java 抓取 https 網頁内容 | 學步園

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class JavaUrl {

public boolean checkWebsite(String checkUrl,String checkContent){

try {

URL    url = new URL(checkUrl);

HttpsURLConnection httpsConn = (HttpsURLConnection)url.openConnection();

//取得該連接配接的輸入流,以讀取響應内容

InputStream ins = httpsConn.getInputStream();

BufferedReader breader = new BufferedReader(new InputStreamReader(ins));

String info = breader.readLine();

long nowTime =new java.util.Date().getTime();

while (info != null) {

if(new java.util.Date().getTime() -nowTime >10000)

return false;//達到10秒就認為逾時

if(info != null && info.indexOf(checkContent)!=-1)

return true;

info = breader.readLine();

}

} catch (Exception e) {

System.out.println("Can't get content:"+checkContent +" from URL:"+checkUrl);

System.out.println("The error is:"+e.getMessage());

e.printStackTrace();

return false;

}

return false;

}

}