この記事では、HTTPツールクラスを実装するJavaのカプセル化操作について説明します。次のように、参照のために共有してください。
HTTPツールクラスの実装:( Apacheパッケージを介して)ファーストクラス
java.io.ioexception; Import org.apache.http.httpentity; Import org.apache.http.httpresponse; Import org.apache.http.httpstatus; Import org.apache.http.clotocolencection; org.apache.http.client.methods.httpget; Import org.apache.http.util.entityutils; Import com.gooagoo.stcu.utils.http.httpclientutils; public class httprequest {private string errormessage; // Information/ *** https String source** @param url* urlアドレス* @return string source**/ public string httprequestString(string url){string result = null; try {httpentity httpentity = httprequest(url); if(httpentity!= null){result = entityutils.tostring(httpentity、 "urf-8"); // utf-8}} catch(ioexception e){errormessage = e.getmessage(); } return result; } / *** http font source** @param url* urlアドレス* @return font source** / public byte [] httprequestbytearray(string url){byte [] result = null; try {httpentity httpentity = httprequest(url); if(httpentity!= null){result = entityutils.tobytearray(httpentity); }} catch(ioException e){errormessage = e.getmessage(); } return result; } / ** * httpを使用してメソッドを取得して * * * @param url * urlアドレス * @return httpenteryアイコン * * / private httpentity httprequest(string url){httpentity result = null; try {httpget httpget = new httpget(url); httpclient httpclient = httpclientutils.gethttpclient(); httpresponse httpresponse; httpresponse = httpclient.execute(httpget); int httpstatuscode = httpresponse.getStatusline()。getStatusCode(); / * * httpが200 */ if(httpstatuscode == httpstatus.sc_ok){result = httpresponse.getentity(); } else {errormessage = "http:" + httpstatuscode; }} catch(clientProtocolexception e){errormessage = e.getMessage(); } catch(ioException e){errormessage = e.getMessage(); } return result; } / *** return message** @return Information** / public String getErrorMessage(){return this.errormessage; }}セカンドクラスの実装:
パッケージcom.demo.http; import java.net.unknownhostexception; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import java.util.map.map.map.map.map.map; apache.http.httpresponse; Import org.apache.http.httpstatus; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http. org.apache.http.client.methods.httpost; Import org.apache.http.impl.client.defaulthttpclient; Import org.apache.http.message.basicnamevaluepair; Import org.apache.http.params.basichtpparams; org.apache.http.params.httpconnectionParams; Import org.apache.http.protocol.http; Import org.apache.http.util.entityutils;パブリッククラスhttpclientutils {private static final request_timeout = 5 * 1000; 1000; //待機中のデータタイムアウトを10秒間設定します// httpclientを初期化し、タイムアウトpublic static httpclient gethttpclient(){basichttpparams httpparams = new basichttpparams(); httpconnectionParams.setConnectionTimeout(httpparams、request_timeout); httpconnectionParams.setsotimeout(httpparams、so_timeout); httpclient client = new defulthttpclient(httpparams);クライアントを返します。 } public static boolean dopost(string url)スロー例外{httpclient client = gethttpclient(); httppost httppost = new httppost(url); HTTPRESPONSE応答。 Response = client.execute(httppost); if(httpstatus.sc_ok == respons.getStatusline()。getStatusCode()){return true; } client.getConnectionManager()。shutdown(); falseを返します。 } / ** *リモートとの返品値の投稿メソッド * * @param hashmap * @param url * @return * / public static string gethtttpxml(hashmap <string、string> hashmap、string url){string responsemsg = ""; httppost request = new httppost(url); List <NameValuePair> params = new ArrayList <NameValuePair>(); iterator <map.entry <string、string >> iter = hashmap.entryset().iterator(); while(iter.hasnext()){entry <string、string> entry = iter.next(); params.add(new BasicNameValuePair(entry.getKey()、entry.getValue())); } try {request.setEntity(new urlencodedformentity(params、http.utf_8)); httpclient client = httpclientutils.gethttpclient(); httpresponse応答= client.execute(request); if(respons.getStatusline()。getStatusCode()== 200){responsemsg = entityutils.toString(respons.getEntity()); }} catch(unknownhostexception e){e.printstacktrace(); } catch(Exception e){e.printstacktrace(); } ressonsemsgを返します。 } / ** *文字列スプライシングパラメーターへのマップ * * @param hashmap * @return * / public static string maptostring(hashmap <string、string> hashmap){string parameterstr = ""; iterator <map.entry <string、string >> iter = hashmap.entryset().iterator(); while(iter.hasnext()){entry <string、string> entry = iter.next(); parameterstr + = "&" + entry.getKey() + "=" + entry.getValue(); } if(paramestr.contains( "&")){parameterstr = parameterstr.replaceFirst( "&"、 "?"); } parameterstrを返します。 }}Java関連のコンテンツの詳細については、このサイトのトピックをご覧ください:「Javaソケットプログラミングスキルの概要」、「Javaファイルとディレクトリの操作スキルの概要」、「Javaデータ構造とアルゴリズムに関するチュートリアル」、「Java Operation Dom Nodeスキルの概要」、Java Cache操作スキルの概要」
この記事がみんなのJavaプログラミングに役立つことを願っています。