この記事は、主にJava Network Crawler Connection Timeoutの問題を次のように研究しています。
ネットワーククローラーでは、多くの場合、次のエラーが発生します。つまり、接続がタイムアウトしました。この問題については、一般的な解決策は次のとおりです。接続時間とリクエスト時間を長時間設定します。接続タイムアウトが発生した場合は、再リケスト[再リケストの数を設定]。
スレッド「Main」java.net.connectexception:接続のタイミング:接続の例外
次のコードは、HTTPClientを使用して接続タイムアウトを解決するサンプルプログラムです。プログラムに直接移動します。
パッケージdaili; import java.io.ioexception; import java.net.uri; Import org.apache.http.httprequest; Import org.apache.http.httpresponse; Import org.apache.http.client.client.clientprotocolexceptip; import.htpt.client.client.client.client.client.client.client.client.client.client.client 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; apache.http.impl.client.defaulthttpclient; import org.apache.http.impl.client.defaulthttpclient2; Import org.apache.http.params.httpconnectionParams; Import org.apache.http.http.params.httpparams; org.apache.http.protocol.basichttpcontext; Import org.apache.http.protocol.executionContext; Import org.apache.http.protocol.httpcontext; Import org.apache.http.utiTils;/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Technology*[email protected]*/public class test1 {public static void main(string [] args)throws clientprotocolexception、ioexception、interrupedexception {getRawhtml( "http://club.autohome.com.cn/bbs/forum-c-2098-1.html#pvareaid=103447");} public static string getrawhtml(string url)slow slow slows clientprotocolexception、ioexception、IoException、Initededexexception、Intationedexedexexception httpclient = new defulthttpclient(); httpclient.getParams()。setParameter( "http.protocol.cookie-policy"、cookiepolicy.browser_compatibility); // setパラメーターhttpparamsパラマhttpconnectionParams.setConnectionTimeout(params、6000); httpconnectionparams.setsotimeout(params、6000*20); // Timeout Requests defaulthttrretryhandler dhr = new defaulthttprequestretryhandler(5、true); httpcontextextextextext basichttpcontext(); httprequest request2 =(httprequest)localcontext.getattribute(executioncontext.http_request); httpclient.sethttprequestretryhandler(dhr); basiccookiestore cookiestore = basiccookiestore( 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/52.0.27.0。 Safari/537.36 "); cocitiestore.addcookie(cookie); cookiestore.addcookie(cookie1); localcontext.setattribute(clientcontext.cookie_store、cositeore); httpget request = new httpget(); request.seturi(uri.create(ur. url; httprest); rawhtml = ""; respons = httpclient.execute(request、localcontext); int statusCode = respons.getStatusLine()。getStatUsCode(); //応答ステータスcode system.out.println(statuscode); if(statuscode == 200){// statusCode 200は、応答が成功していることを示します//エンティティコンテンツrawhtml = entityutils.toString(respons.getEntity()); System.out.println(rawhtml); //エンティティコンテンツコンテンツEntityutils.consume(respons.getEntity()); //消費エンティティ} else {// httpentityentityutils.consumeのストリーミングエンティティを閉じる(response.getentity()); 30-minute Break} httpclient.close(); system.out.println(rawhtml); return rawhtml;}}}
結果:
要約します
上記は、Java Web Crawler Connection Timeout Solutionコードに関するこの記事のすべての内容です。すべての人に役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!