Artikel ini menjelaskan operasi enkapsulasi Java yang menerapkan kelas alat HTTP. Bagikan untuk referensi Anda, sebagai berikut:
Implementasi Kelas Alat HTTP: (Melalui Paket Apache) Kelas Pertama
Impor java.io.ioException; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.htprotsatus; import orgache.http.client.clientprotprotolex; import orgache.http.client.clientprotocoConus; import; http.client.clientprotoConEx; org.apache.http.client.httpClient; import org.apache.http.client.methods.httpget; impor org.apache.http.util.entity; // Informasi/ *** Sumber string https** @param url* Alamat URL* @return String Source**/ Public String httpRequestString (string url) {string result = null; coba {httpentity httpentity = httpRequest (url); if (httpentity! = null) {result = entityutils.toString (httpentity, "urf-8"); // Gunakan UTF-8}} catch (ioException e) {errorMessage = e.getMessage (); } hasil pengembalian; } / *** Sumber font http** @param url* alamat url* @return font source** / public byte [] httprequestByTeArray (string url) {byte [] hasil = null; coba {httpentity httpentity = httpRequest (url); if (httpentity! = null) {result = entityutils.tobyteArray (httpentity); }} catch (ioException e) {errorMessage = e.getMessage (); } hasil pengembalian; } / ** * Gunakan http dapatkan metode untuk menemukan * * @param url * alamat url * @return httpentery ikon * * / private httpentity httpRequest (string url) {httpentity result = null; coba {httpget httpget = httpget baru (url); HttpClient httpClient = httpclientutils.gethttpclient (); Httpresponse httpresponse; httpresponse = httpclient.execute (httpget); int httpStatusCode = httpresponse.getStatusLine (). getStatusCode (); / * * Tentukan apakah http adalah 200 */ if (httpStatusCode == httpstatus.sc_ok) {hasil = httpresponse.getEntity (); } else {errorMessage = "http:" + httpstatusCode; }} catch (clientprotocolexception e) {errorMessage = e.getMessage (); } catch (ioException e) {errorMessage = e.getMessage (); } hasil pengembalian; } / *** Kembalikan pesan** @Return Informasi** / string publik getErrormessage () {return this.errorMessage; }}Implementasi Kelas Kedua:
Paket com.demo.http; import java.net.unknownhostexception; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; impor java.util.mapap; impor java.util.util.util; org.apache.http.httpresponse; import org.apache.http.httpstatus; impor org.apache.http.namevaluepair; impor org.apache.http.client.httpclient; impor org.apache.apache.htp.client.httpclient; impor org.apache.htp.client.client.client; org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulThttpClient; import org.apache.http.message.basicnamevaluePair; impor org.apache.htp.parans.basicnamevaluePair; Impor org.apache.htp.parans. org.apache.http.params.httpconnectionParams; import org.apache.http.protocol.http; org.apache.http.util.entityutils; SOLTEMETER PRIVATICE (SOMEOUT/ PRIVATIC SCOTATICE {private static int request_timeout = 5 * 1000; // atur waktu tunggu waktu tunggu selama 10 detik // parsexml static parsexml = parsexml baru (); // inisialisasi httpClient dan atur timeout public static httpClient getHttpClient () {basichttpparams httpparams = barichttpparams baru (); Httpconnectionparams.setConnectionTimeout (httpparams, request_timeout); Httpconnectionparams.setsoTimeout (httpparams, so_timeout); HttpClient client = DEFAULTHTTPCLIENT baru (httpparams); klien kembali; } public static boolean dopost (string url) melempar pengecualian {httpclient client = getHttpClient (); Httppost httppost = httpppost baru (url); Respons httpresponse; respons = client.execute (httppost); if (httpstatus.sc_ok == response.getstatusLine (). getStatusCode ()) {return true; } client.getConnectionManager (). shutdown (); mengembalikan false; } / ** * Metode posting untuk interaksi nilai pengembalian dengan jarak jauh * * @param hashmap * @param url * @return * / public static string getHttpxml (hashmap <string, string> hashmap, string url) {string responsemsg = ""; Permintaan httppost = httpppost baru (url); Daftar <namevaluePair> params = ArrayList baru <namevaluePair> (); Iterator <map.entry <string, string >> iter = hashmap.entryset () .iterator (); while (iter.hasnext ()) {entri <string, string> entri = iter.next (); params.add (basicnamevaluePair baru (entry.getKey (), entri.getValue ())); } coba {request.setEntity (UrlencodedFormentity baru (params, http.utf_8)); Httpclient client = httpclientutils.gethttpclient (); Httpresponse response = client.execute (request); if (response.getstatusLine (). getStatusCode () == 200) {responsemsg = entityutils.toString (response.getEntity ()); }} catch (unknownHostException e) {e.printstacktrace (); } catch (Exception e) {E.PrintStackTrace (); } return responseSmsg; } / ** * Peta ke parameter splicing string * * @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 ()) {entri <string, string> entri = iter.next (); parameterStr + = "&" + entry.getKey () + "=" + entri.getValue (); } if (paramestr.contains ("&")) {parameterstr = parameterstr.replacefirst ("&", "?"); } return parameterStr; }}For more information about Java related content, please check out the topics of this site: "Summary of Java Socket Programming Skills", "Summary of Java File and Directory Operation Skills", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
Saya harap artikel ini akan membantu pemrograman Java semua orang.