Java实现Http工具类的封装操作示例
程序员文章站
2023-12-16 16:13:04
本文实例讲述了java实现http工具类的封装操作。分享给大家供大家参考,具体如下:
http工具类的实现:(通过apache包)第一个类
import jav...
本文实例讲述了java实现http工具类的封装操作。分享给大家供大家参考,具体如下:
http工具类的实现:(通过apache包)第一个类
import java.io.ioexception; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import 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; // 錯誤信息 /** * http請求字符串資源 * * @param url * url地址 * @return 字符串資源 * */ 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請求字節數組資源 * * @param url * url地址 * @return 字節數組資源 * */ 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 get方式請求 * * @param url * url地址 * @return httpentiry對象 * */ 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 錯誤信息 * */ 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.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.apache.http.params.basichttpparams; import org.apache.http.params.httpconnectionparams; import org.apache.http.protocol.http; import org.apache.http.util.entityutils; public class httpclientutils { private static final int request_timeout = 5 * 1000;// 设置请求超时10秒钟 private static final int so_timeout = 10 * 1000; // 设置等待数据超时时间10秒钟 // static parsexml parsexml = new parsexml(); // 初始化httpclient,并设置超时 public static httpclient gethttpclient() { basichttpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams, request_timeout); httpconnectionparams.setsotimeout(httpparams, so_timeout); httpclient client = new defaulthttpclient(httpparams); return client; } public static boolean dopost(string url) throws exception { httpclient client = gethttpclient(); httppost httppost = new httppost(url); httpresponse response; response = client.execute(httppost); if (httpstatus.sc_ok == response.getstatusline().getstatuscode()) { return true; } client.getconnectionmanager().shutdown(); return false; } /** * 与远程交互的返回值post方式 * * @param hashmap * @param url * @return */ public static string gethttpxml(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 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 responsemsg; } /** * map转字符串 拼接参数 * * @param hashmap * @return */ public static string maptostring(hashmap<string, string> hashmap) { string paramestr = ""; iterator<map.entry<string, string>> iter = hashmap.entryset() .iterator(); while (iter.hasnext()) { entry<string, string> entry = iter.next(); paramestr += "&" + entry.getkey() + "=" + entry.getvalue(); } if (paramestr.contains("&")) { paramestr = paramestr.replacefirst("&", "?"); } return paramestr; } }
更多关于java相关内容感兴趣的读者可查看本站专题:《java socket编程技巧总结》、《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。