注:次のコードは、httpclient4.5.2に基づいています。
Javaのhttpclientを使用して、WebページのGet Request Crawlingを実装する方が簡単な作業です。
public static string get(string url){closeablehttpresponsesponse = null; bufferedreader in = null;文字列結果= ""; try {closeablehttpclienthttppclient = httpclient.createdefault(); httpgethttpget = new httpget(url); Response = httppclient.execute(httpget); in = new BufferedReader(new inputStreamReader(respons.getEntity()。getContent())); stringbuffersb = new StringBuffer( "");文字列line = ""; string nl = system.getProperty( "line.separator"); while((line = in.readline())!= null){sb.append(line + nl); } in.close(); result = sb.toString(); } catch(ioexception e){e.printstacktrace(); }最後に{try {if(null!= response)respons.close(); } catch(ioexception e){e.printstacktrace(); }} return result; }上記の方法は、Get Requestsのマルチスレッド実行を実行する場合にも役立ちます。ただし、このマルチスレッドリクエストは、GETメソッドが呼び出されるたびにHTTPClientインスタンスを作成することに基づいています。各httpClientインスタンスは1回リサイクルされます。これは明らかに最適な実装ではありません。
HTTPClientは、マルチスレッドリクエストソリューションを提供し、公式ドキュメント「Pooling Connection Manager」を表示できます。 HTTPCLIENT実装マルチスレッドリクエストは、組み込みの接続プールに基づいて実装されます。これには、HTTPCLIENT接続プールの管理を担当するキークラス、つまりPoolingHTTPClientConnectionManagerがあります。 PoolinghttpclientConnectionManagerには、2つの重要な方法があります:SetMaxtotalとsetDefaultMaxperroute。 setMaxtotal接続プールへの接続の最大数を設定し、setDefaultMaxperrouteは各ルートのデフォルトの接続数を設定します。また、メソッドsetMaxperroute-このように、特定のサイトの接続の最大数を個別に設定します。
httphosthost = new httphost( "locahost"、80); cm.setmaxperroute(new httproute(host)、50);
Get Requestの実装は、ドキュメントに従ってわずかに調整されています。
パッケージcom.zhyea.robin; org.apache.http.client.methods.closeablehttpresponse; Import org.apache.http.client.methods.httpget; Import org.apache.http.impl.client.closablehttpclient; import org.apache.http.impl.client.httpclient; org.apache.http.impl.conn.poolinghttpclientConnectionManager; java.io.bufferedreader; Import java.io.ioexception; Import java.io.inputStreamReader; public class httputil {private static closeablehttpclienthttpclient; static {poolinghttpclientconnectionmanagercm = new poolinghttpclientConnectionManager(); cm.setmaxtotal(200); cm.setDefaultMaxperroute(20); cm.setDefaultMaxperroute(50); httpclient = httpclients.custom()。setConnectionManager(cm).build(); } public static string get(string url){closeablehttpresponsponse = null; bufferedreaderin = null;文字列結果= ""; try {httpgethttpget = new httpget(url);応答= httpclient.execute(httpget); in = new BufferedReader(new inputStreamReader(respons.getEntity()。getContent())); stringbuffersb = new StringBuffer( "");文字列line = ""; string nl = system.getProperty( "line.separator"); while((line = in.readline())!= null){sb.append(line + nl); } in.close(); result = sb.toString(); } catch(ioexception e){e.printstacktrace(); }最後に{try {if(null!= response)respons.close(); } catch(ioexception e){e.printstacktrace(); }} return result; } public static void main(string [] args){system.out.println(get( "https://www.baidu.com/")); }}それについてです。しかし、私にとっては、http pclientの流fluentな実装を好みます。たとえば、実装したばかりのHTTP Getリクエストは、このように実装できます。
パッケージcom.zhyea.robin; Import org.apache.http.client.fluent.request; Import java.io.ioexception; public class httputil {public static string get(string url){string result = ""; try {result = request.get(url).connecttimeout(1000).sockettimeout(1000).execute()。returncontent()。asstring(); } catch(ioexception e){e.printstacktrace(); } return result; } public static void main(string [] args){system.out.println(get( "https://www.baidu.com/")); }}私たちがしなければならないことは、以前のHTTPCLIENT依存関係をFluent-HC依存関係に置き換えることです。
<Dependency> groupId> org.apache.httpComponents </groupId> <artifactid> fluent-hc </artifactid> <バージョン> 4.5.2 </version> </dependency>
そして、この流fluentな実装は、PoolinghttpclientConnectionManagerを使用して自然に達成されます。設定したMaxtotalおよびdefaultmaxperrouteの値はそれぞれ200と100です。
connmgr = new PoolingHttpClientConnectionManager(SFR); connmgr.setDefaultMaxperroute(100); connmgr.setmaxtotal(200);
人々が動揺させる唯一のことは、エグゼキューターがこれらの2つの値を調整する方法を提供しないということです。しかし、これで十分です。それが実際に機能しない場合は、エグゼキューターメソッドの書き換えを検討し、エグゼキューターを直接使用してGETリクエストを実行することもできます。
executor.newinstance()。execute(request.get(url)).returncontent()。asstring();
それだけです!