บทความนี้อธิบายการดำเนินการห่อหุ้มของ Java ที่ใช้คลาสเครื่องมือ HTTP แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
การใช้งานคลาสเครื่องมือ HTTP: (ผ่านแพ็คเกจ Apache) ชั้นหนึ่ง
นำเข้า java.io.ioException; นำเข้า org.apache.http.htttentity; นำเข้า org.apache.http.httpresponse; นำเข้า org.apache.http.httpstatus; นำเข้า org.apache.http.client.client.clientc org.apache.http.client.methods.httpget; นำเข้า org.apache.http.util.entityutils; นำเข้า com.gooagoo.stcu.utils.http.httpClientils; // ข้อมูล/ *** แหล่งสตริง https** @param url* ที่อยู่ URL* @return สตริงแหล่งที่มา**/ สตริงสาธารณะ httpRequestString (rl string) {สตริงผลลัพธ์ = null; ลอง {httpentity httpentity = httprequest (url); if (httpentity! = null) {result = entityUtils.toString (httpentity, "urf-8"); // ใช้ utf-8}} catch (ioexception e) {errorMessage = e.getMessage (); } ผลตอบแทนผลลัพธ์; } / *** แหล่งที่มาของตัวอักษร http** @param url* ที่อยู่ URL* @return Font Source** / public byte [] httprequestByTearray (url string) {byte [] result = null; ลอง {httpentity httpentity = httprequest (url); if (httpentity! = null) {result = entityUtils.tobytearray (httpentity); }} catch (ioexception e) {errorMessage = e.getMessage (); } ผลตอบแทนผลลัพธ์; } / ** * ใช้ HTTP GET Method เพื่อค้นหา * * @param url * ที่อยู่ URL * @return Httpentery ไอคอน * * / ส่วนตัว 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 ข้อมูล** / สตริงสาธารณะ geterRorMessage () {return this.errorMessage; -การใช้งานชั้นสอง:
แพ็คเกจ com.demo.http; นำเข้า java.net.unknownhostexception; นำเข้า java.util.arraylist; นำเข้า java.util.hashmap; นำเข้า java.util.iterator; นำเข้า java.util.list; org.apache.http.httpresponse; นำเข้า org.apache.http.httpstatus; นำเข้า org.apache.http.namevaluepair; นำเข้า org.apache.http.client.httpclient org.apache.http.client.methods.httppost; นำเข้า org.apache.http.impl.client.defaulthttpClient; นำเข้า org.apache.http.message.basicamevaluepair org.apache.http.params.httpconnectionparams นำเข้า org.apache.http.protocol.http; นำเข้า org.apache.http.util.entityutils; 1,000; // ตั้งค่าการหมดเวลาการรอข้อมูลเป็นเวลา 10 วินาที // parsexml คงที่ parsexml = new parsexml (); // เริ่มต้น httpClient และตั้งค่าการหมดเวลาสาธารณะคงที่ httpClient gethttpClient () {basichttpparams httpparams = ใหม่ basichttpparams (); httpconnectionparams.setconnectiontimeout (httpparams, request_timeout); httpconnectionparams.setsotimeout (httpparams, so_timeout); ไคลเอนต์ httpClient = ใหม่ defaulthttpClient (httpparams); ไคลเอนต์ส่งคืน; } public boolean public boolean dopost (String 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 * / สตริงคงที่สาธารณะ gethttpxml (hashmap <สตริงสตริง> hashmap, url สตริง) {string responsemsg = ";"; httppost request = new httppost (url); รายการ <namevaluePair> params = new ArrayList <NamevaluePair> (); Iterator <map.entry <string, string >> iter = hashmap.entryset () .Iterator (); ในขณะที่ (iter.hasnext ()) {entry <string, string> entry = iter.next (); params.add (ใหม่ BasicNameValuePair (entry.getKey (), entry.getValue ())); } ลอง {request.setEntity (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 (exception e) {e.printstacktrace (); } return responsemsg; } / ** * แผนที่ไปยังพารามิเตอร์การประกบสตริง * * @param hashmap * @return * / สตริงคงที่สาธารณะ maptoString (hashmap <string, string> hashmap) {string parameterster = ""; Iterator <map.entry <string, string >> iter = hashmap.entryset () .Iterator (); ในขณะที่ (iter.hasnext ()) {entry <string, string> entry = iter.next (); ParametERSTR + = "&" + entry.getKey () + "=" + entry.getValue (); } if (paramestr.contains ("&")) {parameterstr = parameterstr.replacefirst ("&", "?"); } return parameterstr; -สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเนื้อหาที่เกี่ยวข้องกับ Java โปรดตรวจสอบหัวข้อของเว็บไซต์นี้: "สรุปทักษะการเขียนโปรแกรม Java Socket", "บทสรุปของไฟล์ Java และทักษะการดำเนินงานไดเรกทอรี", "การสอนเกี่ยวกับโครงสร้างข้อมูล Java และอัลกอริทึม", "สรุปทักษะการดำเนินงาน Java
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน