博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
post 封装Map 发送请求
阅读量:6160 次
发布时间:2019-06-21

本文共 7809 字,大约阅读时间需要 26 分钟。

package com.j1.weixin.util;import java.io.IOException;import java.util.Map;import java.util.Set;import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.log4j.Logger;public class HttpUtils {    /**     * 发送HTTP请求     *     * @param url     * @param propsMap 发送的参数     */    public static HttpResponse httpPost(String url, Map
propsMap) { HttpResponse response = new HttpResponse(); String responseMsg = ""; HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url);// POST请求 if (propsMap != null) { // 参数设置 Set
keySet = propsMap.keySet(); NameValuePair[] postData = new NameValuePair[keySet.size()]; int index = 0; for (String key : keySet) { postData[index++] = new NameValuePair(key, propsMap.get(key) .toString()); } postMethod.addParameters(postData); } postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); try { int statusCode = httpClient.executeMethod(postMethod);// 发送请求 response.setStatusCode(statusCode); if (statusCode == HttpStatus.SC_OK) { // 读取内容 byte[] responseBody = postMethod.getResponseBody(); // 处理返回的内容 responseMsg = new String(responseBody, "utf-8"); } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { postMethod.releaseConnection();// 关闭连接 } response.setContent(responseMsg); return response; } /** * 发送HTTP请求 * * @param url */ public static HttpResponse httpGet(String url) { HttpResponse response = new HttpResponse(); String responseMsg = ""; HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); try { int statusCode = httpClient.executeMethod(getMethod);// 发送请求 response.setStatusCode(statusCode); if (statusCode == HttpStatus.SC_OK) { // 读取内容 byte[] responseBody = getMethod.getResponseBody(); // 处理返回的内容 responseMsg = new String(responseBody, "utf-8"); } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { getMethod.releaseConnection();// 关闭连接 } response.setContent(responseMsg); return response; } /** * 发送HTTP--GET请求 * * @param url * @param propsMap * 发送的参数 */ public static String httpGetSend(String url) { String responseMsg = ""; HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url);// GET请求 try { // http超时5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); // 设置 get 请求超时为 5 秒 getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); httpClient.executeMethod(getMethod);// 发送请求 // 读取内容 byte[] responseBody = getMethod.getResponseBody(); // 处理返回的内容 responseMsg = new String(responseBody, "utf-8"); } catch (Exception e) { Logger.getLogger(HttpUtils.class).error(e.getMessage()); e.printStackTrace(); } finally { getMethod.releaseConnection();// 关闭连接 } return responseMsg; } /** * 发送HTTP请求 * * @param url * @param propsMap * 发送的参数 */ public static String httpSend(String url, Map
propsMap) { String responseMsg = ""; HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url);// POST请求 // postMethod. // 参数设置 Set
keySet = propsMap.keySet(); NameValuePair[] postData = new NameValuePair[keySet.size()]; int index = 0; for (String key : keySet) { postData[index++] = new NameValuePair(key, propsMap.get(key) .toString()); } postMethod.addParameters(postData); try { httpClient.executeMethod(postMethod);// 发送请求// Log.info(postMethod.getStatusCode()); // 读取内容 byte[] responseBody = postMethod.getResponseBody(); // 处理返回的内容 responseMsg = new String(responseBody); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { postMethod.releaseConnection();// 关闭连接 } return responseMsg; } /** * 发送Post HTTP请求 * * @param url * @param propsMap * 发送的参数 * @throws IOException * @throws HttpException */ public static PostMethod httpSendPost(String url, Map
propsMap,String authrition) throws Exception { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url);// POST请求 postMethod.addRequestHeader("Authorization",authrition); postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8"); // 参数设置 Set
keySet = propsMap.keySet(); NameValuePair[] postData = new NameValuePair[keySet.size()]; int index = 0; for (String key : keySet) { postData[index++] = new NameValuePair(key, propsMap.get(key) .toString()); } postMethod.addParameters(postData); httpClient.executeMethod(postMethod);// 发送请求 return postMethod; } /** * 发送Post HTTP请求 * * @param url * @param propsMap * 发送的参数 * @throws IOException * @throws HttpException */ public static PostMethod httpSendPost(String url, Map
propsMap) throws Exception { String responseMsg = ""; HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url);// POST请求 postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8"); // 参数设置 Set
keySet = propsMap.keySet(); NameValuePair[] postData = new NameValuePair[keySet.size()]; int index = 0; for (String key : keySet) { postData[index++] = new NameValuePair(key, propsMap.get(key) .toString()); } postMethod.addParameters(postData); httpClient.executeMethod(postMethod);// 发送请求 return postMethod; } /** * 发送Get HTTP请求 * * @param url * @param propsMap * 发送的参数 * @throws IOException * @throws HttpException */ public static GetMethod httpSendGet(String url, Map
propsMap) throws Exception { String responseMsg = ""; HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url);// GET请求 getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8"); // 参数设置 Set
keySet = propsMap.keySet(); NameValuePair[] postData = new NameValuePair[keySet.size()]; int index = 0; for (String key : keySet) { getMethod.getParams().setParameter(key, propsMap.get(key) .toString()); } httpClient.executeMethod(getMethod);// 发送请求 return getMethod; } }

 

转载地址:http://vnafa.baihongyu.com/

你可能感兴趣的文章
向上扩展型SSD 将可满足向外扩展需求
查看>>
虚机不能启动的特例思考
查看>>
SQL Server编程系列(1):SMO介绍
查看>>
在VMware网络测试“专用VLAN”功能
查看>>
使用Formik轻松开发更高质量的React表单(三)<Formik />解析
查看>>
也问腾讯:你把用户放在什么位置?
查看>>
CSS Sprites 样式生成工具(bg2css)
查看>>
[转]如何重构代码--重构计划
查看>>
类中如何对list泛型做访问器??
查看>>
C++解析XML--使用CMarkup类解析XML
查看>>
P2P应用层组播
查看>>
Sharepoint学习笔记—修改SharePoint的Timeouts (Execution Timeout)
查看>>
CSS引入的方式有哪些? link和@import的区别?
查看>>
Redis 介绍2——常见基本类型
查看>>
asp.net开发mysql注意事项
查看>>
(转)Cortex-M3 (NXP LPC1788)之EEPROM存储器
查看>>
ubuntu set defult jdk
查看>>
[译]ECMAScript.next:TC39 2012年9月会议总结
查看>>
【Xcode】编辑与调试
查看>>
用tar和split将文件分包压缩
查看>>