이 기사에서는 HTTP 도구 클래스를 구현하는 Java의 캡슐화 작업에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
HTTP 도구 클래스 구현 : (Apache 패키지를 통해) 첫 번째 클래스
import java.io.ioexception; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.htttp.htttp.httpstatus; import org.apache.http.client.clientprotocolexception; import org.apache.http org.apache.http.client.methods.httpget; import org.apache.http.util.entityutils; import com.gooagoo.stcu.utils.http.htttp.httpclientutils; public class httprequest {private String errormessage; // information/ *** https 문자열 소스** @param url* url 주소* @return string source**/ public string httprequeststring (String URL) {String result = null; {httpentity httpentity = httprequest (url); if (httpentity! = null) {result = entityutils.toString (httpentity, "urf-8"); // UTF-8}} catch (ioException e) {errormessage = e.getMessage (); } 반환 결과; } / *** http font source** @param url* url 주소* @return font source** / public byte [] httprequestbyTearRay (String URL) {byte [] result = null; {httpentity httpentity = httprequest (url); if (httpentity! = null) {result = entityutils.tobytearray (httpentity); }} catch (ioexception e) {errormessage = e.getMessage (); } 반환 결과; } / ** * http를 사용하여 * * @param url * url 주소 * @return httpentery icon * / private httpentity httprequest (문자열 URL) {httpentity result = null; {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 Information* / public string getErrorMessage () {return this.errorMessage; }}두 번째 클래스 구현 :
package 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.entry; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlededforment org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.htttp.message.basicnamevaluepair; import org.apache.http.params.basichttpparams; org.apache.http.params.httpconnectionparams; import org.apache.http.protocol.http; import org.apache.http.util.entityutils; 공동 클래스 httpclientutils {private static final int _time _ 5 * 1000; 1000; // 10 초 동안 대기 대기 데이터 시간 초과 설정 // 정적 parsexml parsexml = new parsexml (); // httpclient 초기화 및 설정 시간 초과 공개 정적 httpclient gethttpclient () {basichttpparams httpparams = new Basichtpparams (); httpconnectionparams.setConnectionTimeout (httpparams, request_timeout); httpconnectionparams.setsotimeout (httpparams, so_timeout); httpclient client = new defaulthttpclient (httpparams); 반환 클라이언트; } public static boolean dopost (문자열 URL) 예외 {httpclient client = gethttpclient (); httppost httppost = 새로운 httppost (url); httpresponse 응답; 응답 = client.execute (httppost); if (httpstatus.sc_ok == response.getStatusline (). getStatuscode ()) {return true; } client.getConnectionManager (). shutdown (); 거짓을 반환합니다. } / ** * 원격 * @param hashmap * @param url * @return * / public static string gethtttpxml (hashmap <string, string> hashmap, string url)과의 반환 값 상호 작용을위한 포스트 메소드 httppost request = new Httppost (URL); List <NamesValuePair> params = new ArrayList <NamesValuePair> (); iterator <map.entry <string, string >> iter = hashmap.entryset () .eritator (); 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 response = client.execute (요청); if (response.getStatusline (). getStatusCode () == 200) {responsemsg = entityUtils.toString (response.getEntity ()); }} catch (unknownHostException e) {e.printstacktrace (); } catch (예외 e) {e.printstacktrace (); } returnEmsg; } / ** * 문자열 스 플라이 싱 매개 변수 * * @param hashmap * @return * / public static string maptostring (Hashmap <String, String> hashmap) {String parameterstr = ""; iterator <map.entry <string, string >> iter = hashmap.entryset () .eritator (); while (iter.hasnext ()) {entry <string, string> entry = iter.next (); parameterstr + = "&" + entery.getKey () + "=" + eTry.GetValue (); } if (parameSt.contains ( "&")) {parameterstr = parameterstr.replaceFirst ( "&", "?"); } return parameterstr; }}Java 관련 컨텐츠에 대한 자세한 내용은이 사이트의 주제를 확인하십시오 : "Java 소켓 프로그래밍 기술 요약", "Java 파일 및 디렉토리 운영 기술 요약", "Java 데이터 구조 및 알고리즘에 대한 자습서", "Java 운영 Dom Node Skills의 요약"및 "Java Cache 운영 기술의 요약".
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.