Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)

1 1 package com.ad.ssp.engine.common; 2 2 3 3 import java.io.IOException; 4 4 import java.util.ArrayList; 5 5 import java.util.List; 6 6 import java.util.Map; 7 7 import java.util.Map.Entry; 8 8 9 9 import org.apache.http.Header; 10 10 import org.apache.http.entity.ByteArrayEntity; 11 11 import org.apache.http.entity.ContentType; 12 12 import org.apache.http.entity.StringEntity; 13 13 import org.apache.http.message.BasicHeader; 14 14 import org.apache.logging.log4j.LogManager; 15 15 import org.apache.logging.log4j.Logger; 16 16 17 17 import com.adwm.lib.http.HttpClient; 18 18 import com.adwm.lib.http.HttpResult; 19 19 20 20 /** 21 21 * 对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高 22 22 * 23 23 * 24 24 */ 25 25 public class HttpClientUtil { 26 26 private static final Logger logger = LogManager.getLogger(HttpClientUtil.class); 27 27 28 28 // private static HttpClient client = new HttpClient(600); 29 29 private static HttpClient client = new HttpClient(); 30 30 31 31 public static HttpClient getInstance() { 32 32 return client; 33 33 } 34 34 35 35 /** 36 36 * form方式提交http post请求 37 37 * 38 38 * @param targetUrl 39 39 * 目标url地址 40 40 * @param headers 41 41 * header请求参数集合 42 42 * @param postParams 43 43 * body请求参数集合 44 44 * @param timeout 45 45 * 超时时间(单位为毫秒) 46 46 * @return 返回的http body体经utf8编码后的字符串 47 47 */ 48 48 public static String postKVParams1(String targetUrl, Map<String, String> headers, Map<String, Object> postParams, 49 49 int timeout) { 50 50 List<Header> headerList = getHeaderList(headers); 51 51 try { 52 52 HttpResult hr = client.post(targetUrl, headerList, postParams, timeout); 53 53 if (hr.getStatus() == 200) 54 54 return hr.getContentAsString(); 55 55 else { 56 56 logger.warn("The request url is: {}, its reponse code is: {}", targetUrl, hr.getStatus()); 57 57 } 58 58 } catch (IOException e) { 59 59 // TODO Auto-generated catch block 60 60 e.printStackTrace(); 61 61 } 62 62 63 63 return null; 64 64 } 65 65 66 66 private static List<Header> getHeaderList(Map<String, String> headers) { 67 67 if (headers != null && headers.size() > 0) { 68 68 List<Header> headerList = new ArrayList<Header>(); 69 69 for (Entry<String, String> entry : headers.entrySet()) { 70 70 headerList.add(new BasicHeader(entry.getKey(), entry.getValue())); 71 71 } 72 72 return headerList; 73 73 } 74 74 75 75 return null; 76 76 } 77 77 78 78 /** 79 79 * form方式提交http post请求 80 80 * 81 81 * @param targetUrl 82 82 * 目标url地址 83 83 * @param headers 84 84 * header请求参数集合 85 85 * @param postParams 86 86 * body请求参数集合 87 87 * @param timeout 88 88 * 超时时间(单位为毫秒) 89 89 * @return 未处理的HttpResult对象,供调用方自己解析和处理 90 90 */ 91 91 public static HttpResult postKVParams2(String targetUrl, Map<String, String> headers, 92 92 Map<String, Object> postParams, int timeout) { 93 93 List<Header> headerList = getHeaderList(headers); 94 94 try { 95 95 return client.post(targetUrl, headerList, postParams, timeout); 96 96 } catch (IOException e) { 97 97 // TODO Auto-generated catch block 98 98 e.printStackTrace(); 99 99 } 100100 101101 return null; 102102 } 103103 104104 /** 105105 * 用post方法提交json字符串参数 106106 * 107107 * @param targetUrl 108108 * 目标url地址 109109 * @param headers 110110 * header请求参数集合 111111 * @param jsonBody 112112 * json字符串 113113 * @param timeout 114114 * 超时时间(单位为毫秒) 115115 * @return 返回的http body体经utf8编码后的字符串 116116 */ 117117 public static String postJsonParams1(String targetUrl, Map<String, String> headers, String jsonBody, int timeout) throws Exception { 118118 List<Header> headerList = getHeaderList(headers); 119119 120120 StringEntity entity = new StringEntity(jsonBody, "UTF-8"); 121121 entity.setContentType("application/json"); 122122 entity.setContentEncoding("UTF-8"); 123123 HttpResult httpRst = client.post(targetUrl, headerList, entity, timeout); 124124 if(httpRst.getStatus() == ResponseCodeUtil.HTTP_STATUS_OK) 125125 return httpRst.getContentAsString(); 126126 else { 127127 logger.warn("The request url is: {}, its reponse code is: {}", targetUrl, httpRst.getStatus()); 128128 } 129129 130130 return null; 131131 } 132132 133133 /** 134134 * 用post方法提交json字符串参数 135135 * 136136 * @param targetUrl 137137 * 目标url地址 138138 * @param headers 139139 * header请求参数集合 140140 * @param jsonBody 141141 * json字符串 142142 * @param timeout 143143 * 超时时间(单位为毫秒) 144144 * @return 未处理的HttpResult对象,供调用方自己解析和处理 145145 */ 146146 public static HttpResult postJsonParams2(String targetUrl, Map<String, String> headers, String jsonBody, 147147 int timeout) throws Exception { 148148 List<Header> headerList = getHeaderList(headers); 149149 StringEntity entity = new StringEntity(jsonBody, "UTF-8"); 150150 entity.setContentType("application/json"); 151151 return client.post(targetUrl, headerList, entity, timeout); 152152 } 153153 154154 /** 155155 * 通过POST方式发起protocol请求 156156 * @param url 157157 * @param headers 158158 * @param content 159159 * @param timeout 160160 * @return 161161 */ 162162 public static byte[] postProtobuf(String url, Map<String, String> headers, byte[] content, int timeout) throws Exception { 163163 List<Header> headerList = getHeaderList(headers); 164164 ByteArrayEntity entity = new ByteArrayEntity(content, ContentType.APPLICATION_OCTET_STREAM); 165165 HttpResult httpResult = client.post(url, headerList, entity, timeout); 166166 if (httpResult.getStatus() == ResponseCodeUtil.HTTP_STATUS_OK){ 167167 return httpResult.getContent(); 168168 } else { 169169 logger.warn("The request url is: {}, its reponse code is: {}", url, httpResult.getStatus()); 170170 } 171171 return null; 172172 } 173173 174174 /** 175175 * 以get方法请求url 176176 * 177177 * @param targetUrl 178178 * 目标url地址 179179 * @param headers 180180 * header请求参数集合 181181 * @param timeout 182182 * 超时时间(单位为毫秒) 183183 * @return 返回的http body体经utf8编码后的字符串 184184 */ 185185 public static String get1(String targetUrl, Map<String, String> headers, int timeout) throws Exception { 186186 List<Header> headerList = getHeaderList(headers); 187187 HttpResult httpRst = client.get(targetUrl, headerList, timeout); 188188 if(httpRst.getStatus() == ResponseCodeUtil.HTTP_STATUS_OK) 189189 return httpRst.getContentAsString(); 190190 else { 191191 logger.warn("The request url is: {}, its reponse code is: {}", targetUrl, httpRst.getStatus()); 192192 } 193193 return null; 194194 } 195195 196196 /** 197197 * 以get方法请求url 198198 * 199199 * @param targetUrl 200200 * 目标url地址 201201 * @param headers 202202 * header请求参数集合 203203 * @param timeout 204204 * 超时时间(单位为毫秒) 205205 * @return 未处理的HttpResult对象,供调用方自己解析和处理 206206 */ 207207 public static HttpResult get2(String targetUrl, Map<String, String> headers, int timeout) throws Exception { 208208 List<Header> headerList = getHeaderList(headers); 209209 return client.get(targetUrl, headerList, timeout); 210210 } 211211 212212 }
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java常见的 http 请求库比较

java常见的http请求库有httpclient,RestTemplate,OKhttp,更高层次封装的feign、retrofit1、HttpClientHttpClient:代码复杂,还得操心资源回收等。代码很复杂,冗余代码多,不建议直接使用。2、RestTemplateRestTemplate:是Spring提供的用于访问

java面试(1)

1.面向对象的基本特征  封装、继承、多态、  封装:把客观事物封装成类  继承:继承一个类,就可以使用这个类的所有功能,并且在无需编写原来类的情况下对这些功能进行扩展  多态:子对象调用父对象,父对象会根据当前调用的子对象以不同的方式运作  实现多态:覆盖,重载2.final\\finally\\finalize的区别  fin

HttpClient 连接池配置和使用

在游戏项目开发中,经常会向其它的服务发送一些Http请求,获取一些数据或验证。比如充值,SDK验证等。如果每次都重新创建一个新的HttpClient对象的话,当并发上来时,容易出现异常或连接失败,超时。这里可以使用HttpClient的连接池配置,减少HttpClient创建的数量,减少资源开销。packagecom.mygame.common

Java 调用RESTful接口的几种方式

前端一般通过Ajax来调用,后端调用的方式还是挺多的,比如HttpURLConnection,HttpClient,Spring的RestTemplate服务端代码如下:服务端接口请求的URL:http://localhost:8080/rest/user/getUser/xiaoming/18(https://www.oschina.net/a

Java之HttpClient调用WebService接口发送短信源码实战

摘要Java之HttpClient调用WebService接口发送短信源码实战一:接口文档!Java之HttpClient调用WebService接口源码001.png(https://imgblog.csdnimg.cn/img_convert/1e2ea7858d12c11af36e591290ba0496.png)