欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java網(wǎng)絡(luò)爬蟲連接超時解決實例代碼

 更新時間:2018年01月24日 10:22:11   作者:sober_qianyang  
這篇文章主要介紹了java網(wǎng)絡(luò)爬蟲連接超時解決的問題,分享了一則使用httpclient解決連接超時的Java爬蟲實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

本文研究的主要是java網(wǎng)絡(luò)爬蟲連接超時的問題,具體如下。

在網(wǎng)絡(luò)爬蟲中,經(jīng)常會遇到如下報錯。即連接超時。針對此問題,一般解決思路為:將連接時間、請求時間設(shè)置長一下。如果出現(xiàn)連接超時的情況,則在重新請求【設(shè)置重新請求次數(shù)】。

Exception in thread "main" java.net.ConnectException: Connection timed out: connect

下面的代碼便是使用httpclient解決連接超時的樣例程序。直接上程序。

package daili;
import java.io.IOException;
import java.net.URI;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie2;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
/*
 * author:合肥工業(yè)大學(xué) 管院學(xué)院 錢洋 
 *1563178220@qq.com
*/
public class Test1 {
	public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
		getRawHTML("http://club.autohome.com.cn/bbs/forum-c-2098-1.html#pvareaid=103447");
	}
	public static String getRawHTML ( String url ) throws ClientProtocolException, IOException, InterruptedException{
		//初始化
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter("http.protocol.cookie-policy", 
		        CookiePolicy.BROWSER_COMPATIBILITY);
		//設(shè)置參數(shù)
		HttpParams params = httpclient.getParams();
		//連接時間
		HttpConnectionParams.setConnectionTimeout(params, 6000);
		HttpConnectionParams.setSoTimeout(params, 6000*20);
		//超時重新請求次數(shù)
		DefaultHttpRequestRetryHandler dhr = new DefaultHttpRequestRetryHandler(5,true);
		HttpContext localContext = new BasicHttpContext();
		HttpRequest request2 = (HttpRequest) localContext.getAttribute( 
		        ExecutionContext.HTTP_REQUEST);
		httpclient.setHttpRequestRetryHandler(dhr);
		BasicCookieStore cookieStore = new BasicCookieStore();
		BasicClientCookie2 cookie = new BasicClientCookie2("Content-Type","text/html;charset=UTF-8");
		BasicClientCookie2 cookie1 = new BasicClientCookie2("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
		cookieStore.addCookie(cookie);
		cookieStore.addCookie(cookie1);
		localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
		HttpGet request = new HttpGet();
		request.setURI(URI.create(url));
		HttpResponse response = null;
		String rawHTML = "";
		response = httpclient.execute(request,localContext);
		int StatusCode = response.getStatusLine().getStatusCode();
		//獲取響應(yīng)狀態(tài)碼
		System.out.println(StatusCode);
		if(StatusCode == 200){
			//狀態(tài)碼200表示響應(yīng)成功
			//獲取實體內(nèi)容
			rawHTML = EntityUtils.toString (response.getEntity());
			System.out.println(rawHTML);
			//輸出實體內(nèi)容
			EntityUtils.consume(response.getEntity());
			//消耗實體
		} else {
			//關(guān)閉HttpEntity的流實體
			EntityUtils.consume(response.getEntity());
			//消耗實體
			Thread.sleep(20*60*1000);
			//如果報錯先休息30分鐘
		}
		httpclient.close();
		System.out.println(rawHTML);
		return rawHTML;
	}
}

結(jié)果:

總結(jié)

以上就是本文關(guān)于java網(wǎng)絡(luò)爬蟲連接超時解決實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • 詳解Spring Cloud Alibaba Sidecar多語言微服務(wù)異構(gòu)

    詳解Spring Cloud Alibaba Sidecar多語言微服務(wù)異構(gòu)

    這篇文章主要介紹了詳解Spring Cloud Alibaba Sidecar多語言微服務(wù)異構(gòu),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • pagehelper插件顯示total為-1或1的問題

    pagehelper插件顯示total為-1或1的問題

    這篇文章主要介紹了pagehelper插件顯示total為-1或1,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Spring Boot3整合Mybatis Plus的詳細(xì)過程(數(shù)據(jù)庫為MySQL)

    Spring Boot3整合Mybatis Plus的詳細(xì)過程(數(shù)據(jù)庫為MySQL)

    這篇文章主要介紹了Spring Boot3整合Mybatis Plus的詳細(xì)過程(數(shù)據(jù)庫為MySQL),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • Java中方法作為參數(shù)傳遞的方式

    Java中方法作為參數(shù)傳遞的方式

    這篇文章主要介紹了Java如何讓方法作為參數(shù)傳遞,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • java編譯器和JVM的區(qū)別

    java編譯器和JVM的區(qū)別

    在本篇文章里小編給大家整理的是一篇關(guān)于java編譯器和JVM的區(qū)別的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • spring-core組件詳解——PropertyResolver屬性解決器

    spring-core組件詳解——PropertyResolver屬性解決器

    這篇文章主要介紹了spring-core組件詳解——PropertyResolver屬性解決器,需要的朋友可以參考下
    2016-05-05
  • 在Idea2020.1中使用gitee2020.1.0創(chuàng)建第一個代碼庫的實現(xiàn)

    在Idea2020.1中使用gitee2020.1.0創(chuàng)建第一個代碼庫的實現(xiàn)

    這篇文章主要介紹了在Idea2020.1中使用gitee2020.1.0創(chuàng)建第一個代碼庫的實現(xiàn),文中通過圖文示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • idea項目全局去掉嚴(yán)格的語法校驗方式

    idea項目全局去掉嚴(yán)格的語法校驗方式

    這篇文章主要介紹了idea項目全局去掉嚴(yán)格的語法校驗方式,具有很好的參考價值,希望對大家有所幫助。
    2023-04-04
  • mybatis-plus批量更新太慢該如何解決詳解

    mybatis-plus批量更新太慢該如何解決詳解

    這篇文章主要給大家介紹了關(guān)于mybatis-plus批量更新太慢該如何解決的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-03-03
  • Java實現(xiàn)簡易俄羅斯方塊

    Java實現(xiàn)簡易俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)簡易俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06

最新評論