java網(wǎng)絡(luò)爬蟲(chóng)連接超時(shí)解決實(shí)例代碼
本文研究的主要是java網(wǎng)絡(luò)爬蟲(chóng)連接超時(shí)的問(wèn)題,具體如下。
在網(wǎng)絡(luò)爬蟲(chóng)中,經(jīng)常會(huì)遇到如下報(bào)錯(cuò)。即連接超時(shí)。針對(duì)此問(wèn)題,一般解決思路為:將連接時(shí)間、請(qǐng)求時(shí)間設(shè)置長(zhǎng)一下。如果出現(xiàn)連接超時(shí)的情況,則在重新請(qǐng)求【設(shè)置重新請(qǐng)求次數(shù)】。
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
下面的代碼便是使用httpclient解決連接超時(shí)的樣例程序。直接上程序。
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é)院 錢(qián)洋 *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(); //連接時(shí)間 HttpConnectionParams.setConnectionTimeout(params, 6000); HttpConnectionParams.setSoTimeout(params, 6000*20); //超時(shí)重新請(qǐng)求次數(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)成功 //獲取實(shí)體內(nèi)容 rawHTML = EntityUtils.toString (response.getEntity()); System.out.println(rawHTML); //輸出實(shí)體內(nèi)容 EntityUtils.consume(response.getEntity()); //消耗實(shí)體 } else { //關(guān)閉HttpEntity的流實(shí)體 EntityUtils.consume(response.getEntity()); //消耗實(shí)體 Thread.sleep(20*60*1000); //如果報(bào)錯(cuò)先休息30分鐘 } httpclient.close(); System.out.println(rawHTML); return rawHTML; } }
結(jié)果:
總結(jié)
以上就是本文關(guān)于java網(wǎng)絡(luò)爬蟲(chóng)連接超時(shí)解決實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- Java 網(wǎng)絡(luò)爬蟲(chóng)新手入門(mén)詳解
- Java 網(wǎng)絡(luò)爬蟲(chóng)基礎(chǔ)知識(shí)入門(mén)解析
- java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)代碼示例
- hadoop中實(shí)現(xiàn)java網(wǎng)絡(luò)爬蟲(chóng)(示例講解)
- Java實(shí)現(xiàn)爬蟲(chóng)給App提供數(shù)據(jù)(Jsoup 網(wǎng)絡(luò)爬蟲(chóng))
- 基于Java HttpClient和Htmlparser實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)代碼
- 使用java實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)
相關(guān)文章
詳解Spring Cloud Alibaba Sidecar多語(yǔ)言微服務(wù)異構(gòu)
這篇文章主要介紹了詳解Spring Cloud Alibaba Sidecar多語(yǔ)言微服務(wù)異構(gòu),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11pagehelper插件顯示total為-1或1的問(wèn)題
這篇文章主要介紹了pagehelper插件顯示total為-1或1,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Spring Boot3整合Mybatis Plus的詳細(xì)過(guò)程(數(shù)據(jù)庫(kù)為MySQL)
這篇文章主要介紹了Spring Boot3整合Mybatis Plus的詳細(xì)過(guò)程(數(shù)據(jù)庫(kù)為MySQL),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07spring-core組件詳解——PropertyResolver屬性解決器
這篇文章主要介紹了spring-core組件詳解——PropertyResolver屬性解決器,需要的朋友可以參考下2016-05-05在Idea2020.1中使用gitee2020.1.0創(chuàng)建第一個(gè)代碼庫(kù)的實(shí)現(xiàn)
這篇文章主要介紹了在Idea2020.1中使用gitee2020.1.0創(chuàng)建第一個(gè)代碼庫(kù)的實(shí)現(xiàn),文中通過(guò)圖文示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07idea項(xiàng)目全局去掉嚴(yán)格的語(yǔ)法校驗(yàn)方式
這篇文章主要介紹了idea項(xiàng)目全局去掉嚴(yán)格的語(yǔ)法校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-04-04Java實(shí)現(xiàn)簡(jiǎn)易俄羅斯方塊
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06