使用commonshttpclient模拟实现http请求
在开发项目时,经常需要对接第三方平台接口,而且是后台触发请求,比如对接短信平台接口、语音中心平台接口、支付平台接口等等,可以引入commons-httpclient方式进行开发。
1、在pom中添加依赖 3.4 3.1 commons-httpclient commons-httpclient ${http.client.version} org.apache.commons commons-lang3 ${org.apache.commons.lang3}
2、POST请求方法/** * 无 header 参数 * @param url * @param param * @param charset * @param contentType * @return */ public static String sendPost(String url, String param, String charset, String contentType) { String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset; String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType; String result = StringUtils.EMPTY; PostMethod method = null; try { HttpClient client = new HttpClient(); //设置连接超时时间为30秒(连接初始化时间) client.getHttpConnectionManager().getParams().setConnectionTimeout(30000); //带参数的那个请求的访问串 method = new PostMethod(url); // 发送请求 //指定传送字符集为UTF-8格式 RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset); method.setRequestEntity(requestEntity); int statusCode = client.executeMethod(method); if (statusCode == 200) { //解析返回支付Json数据 result = method.getResponseBodyAsString(); } // 关闭连接 client.getHttpConnectionManager().closeIdleConnections(1); return result; } catch (Exception e) { return StringUtils.EMPTY; } finally { if (method != null) { // 释放连接 method.releaseConnection(); } } }/** * 带 header 参数 * @param url * @param param * @param map * @param charset * @param contentType * @return */ public static String sendHeaderPost(String url, String param, Map map, String charset, String contentType) { String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset; String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType; String result = StringUtils.EMPTY; PostMethod method = null; try { HttpClient client = new HttpClient(); // 设置连接超时时间为30秒(连接初始化时间) client.getHttpConnectionManager().getParams().setConnectionTimeout(30000); // 带参数的那个请求的访问串 method = new PostMethod(url); // 设置消息头 if (CollectionUtility.isNotEmpty(map)) { for (Map.Entry entry : map.entrySet()) { method.addRequestHeader(entry.getKey(), entry.getValue()); } } // 发送请求 指定传送字符集为UTF-8格式 RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset); method.setRequestEntity(requestEntity); int statusCode = client.executeMethod(method); if (statusCode == 200) { //解析返回支付Json数据 result = method.getResponseBodyAsString(); } // 关闭连接 client.getHttpConnectionManager().closeIdleConnections(1); return result; } catch (Exception e) { return StringUtils.EMPTY; } finally { if (method != null) { // 释放连接 method.releaseConnection(); } } }
3、完整的工具类import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.lang.StringUtils; import java.util.Map; /** * http调用POST请求 */ public class HttpClientUtility { static final String CHARSET_UTF8 = "utf-8"; static final String CONTENT_TYPE_JSON = "application/json"; /** * 默认编码utf-8,content-type为application/json * @param url * @param param * @return */ public static String sendPost(String url, String param) { return sendPost(url, param, CHARSET_UTF8, CONTENT_TYPE_JSON); } /** * 默认编码utf-8 指定content-type为application/json * @param url * @param param * @param contentType * @return */ public static String sendPost(String url, String param, String contentType) { String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType; return sendPost(url, param, CHARSET_UTF8, type); } /** * 指定header参数 * @param url * @param param * @param map * @param contentType * @return */ public static String sendHeaderPost(String url, String param, Map map, String contentType) { String type = StringUtility.isNotEmpty(contentType) ? CONTENT_TYPE_JSON : contentType; return sendHeaderPost(url, param, map, CHARSET_UTF8, type); } /** * 无 header 参数 * @param url * @param param * @param charset * @param contentType * @return */ public static String sendPost(String url, String param, String charset, String contentType) { String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset; String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType; String result = StringUtils.EMPTY; PostMethod method = null; try { HttpClient client = new HttpClient(); //设置连接超时时间为30秒(连接初始化时间) client.getHttpConnectionManager().getParams().setConnectionTimeout(30000); //带参数的那个请求的访问串 method = new PostMethod(url); // 发送请求 //指定传送字符集为UTF-8格式 RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset); method.setRequestEntity(requestEntity); int statusCode = client.executeMethod(method); if (statusCode == 200) { //解析返回支付Json数据 result = method.getResponseBodyAsString(); } // 关闭连接 client.getHttpConnectionManager().closeIdleConnections(1); return result; } catch (Exception e) { return StringUtils.EMPTY; } finally { if (method != null) { // 释放连接 method.releaseConnection(); } } } /** * 带 header 参数 * @param url * @param param * @param map * @param charset * @param contentType * @return */ public static String sendHeaderPost(String url, String param, Map map, String charset, String contentType) { String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset; String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType; String result = StringUtils.EMPTY; PostMethod method = null; try { HttpClient client = new HttpClient(); // 设置连接超时时间为30秒(连接初始化时间) client.getHttpConnectionManager().getParams().setConnectionTimeout(30000); // 带参数的那个请求的访问串 method = new PostMethod(url); // 设置消息头 if (CollectionUtility.isNotEmpty(map)) { for (Map.Entry entry : map.entrySet()) { method.addRequestHeader(entry.getKey(), entry.getValue()); } } // 发送请求 指定传送字符集为UTF-8格式 RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset); method.setRequestEntity(requestEntity); int statusCode = client.executeMethod(method); if (statusCode == 200) { //解析返回支付Json数据 result = method.getResponseBodyAsString(); } // 关闭连接 client.getHttpConnectionManager().closeIdleConnections(1); return result; } catch (Exception e) { return StringUtils.EMPTY; } finally { if (method != null) { // 释放连接 method.releaseConnection(); } } } }
让我们带您了解各种商用洗碗机以商用洗碗机的分类为标准,推荐商用洗碗机的常见类型如下。揭盖式洗碗机清洗方法是将餐具装筐后放入洗碗机中,用高压热水喷淋清洗消毒。每次洗涤1015筐,约500个餐具小时,洗涤时间1分
巴菲特的三顿失败的午餐,受伤的只是巴菲特吗?图片来自网络,侵删众所周知,巴菲特的午餐(跟巴菲特共进午餐)绝对是一种奢侈品,中国则是奢侈品消费大国。但为什么网上有篇文章会说巴菲特宣布跟三个中国人吃饭后,我永远不跟中国人吃饭了?
我们的饮食是不是比商纣王强多了?是我在知乎的回答。跑个题。不说饮食,昨天,我一个普通人在十八楼的办公室拍的。纣王的摘星楼能有多高?国外有人说,现代人(你我这样的普通人)在物质生活上的幸福指数,高于几百年前的教皇他
唉!雷军的一个字文案最近,雷军的一个字(语气词)唉成为热词。这说明,大品牌的文案,怎么做都好。(小马宋也这么说过)有一个广为流传的关于鲁迅的段子鲁迅晚安!语文老师晚安中晚字点明了时间,令人联想到天色已
商用洗碗机的选择要注意哪些方面洗碗工目前很难招到。使用洗碗机后,餐具能及时清洗干净,立即投入使用。一般只需1人。过去需要5个人手动洗碗,现在只需要23个人在过去,一般需要23个小时,以手动洗碗。现在完成同样的工
食堂洗碗机的设备特点食堂洗碗机能保证按时洗碗的质量。全自动洗碗机干净卫生。它不仅节省了人工成本,而且值得信赖。食堂的洗碗机快速清洗而且没有残留,可安全使用餐具,防止细菌交叉感染。食堂的洗碗机采用食品级
食堂洗碗机的操作规程学校食堂宾馆饭店餐饮连锁店中央厨房厨房等由于食客众多,使用餐具数量大,给洗碗工洗涤餐具带来很大的工作量,用洗碗机的工作量也很大。让我们了解一下食堂的洗碗机安全操作规程。检查机器运转
餐饮业为什么使用商用洗碗机?1。提高洗涤速度酒店洗碗机的使用时间明显缩短,降低了员工的劳动强度和成本。2。降低损伤率餐馆里餐具的使用量一直在增加,机器洗碗不像人工洗碗那样磕磕碰碰,因此破损率也低。3。减少员工
自动洗碗机清洗餐具时水温的重要性为了省时省电,清洁餐具,应根据餐具的数量和餐具的脏污程序选择水温。洗碗机的控制机构一般包括程序控制器和选择开关。常温范围更适合人口较少的家庭,因为平时餐具用量不多,餐具上的油渍也可
商用洗碗机的分类及特点洗碗机是一种能自动清洗碗筷盘子碟子刀叉等餐具的设备。市场上的自动洗碗机可分为家用和商用两大类,家用自动洗碗机只适用于家庭,主要是橱柜台式机水槽一体机和一体机。商用洗碗机按结构可分为
农村1家人随手种一分地,3个月不买菜,比城里买的好吃几倍随着时间的推移,眼看现在即将进入1月份了,2个多月前种的菜慢慢也快要过季了,只能在它最后的时光里趁热吃了。蜜蜂正在采蜜每年的10月份,都会有不少喜欢吃自己种的菜的农人家在地里田里菜