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

本文共 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://vcizo.baihongyu.com/

你可能感兴趣的文章
SSM——查询_分页
查看>>
梯度下降(Gradient descent)
查看>>
Windows平台分布式架构实践 - 负载均衡
查看>>
如何让LinearLayout也有类似Button的点击效果?
查看>>
JAVA读取文件方法大全
查看>>
寻找最小的k个数
查看>>
CSS3中的动画效果记录
查看>>
CI框架整合微信公共平台接口
查看>>
XCode快捷键
查看>>
request.getScheme()的使用方法
查看>>
Kaldi单音素模型 训练部分
查看>>
Android快速开发常用知识点系列目录
查看>>
Java ActiveMQ队列模式案例
查看>>
EJB2的配置
查看>>
最容易理解的对卷积(convolution)的解释
查看>>
《机器学习实战》知识点笔记目录
查看>>
Linux操作系统实时性分析
查看>>
mysql导出导入所有数据库
查看>>
[转载]数据库缓存算法思想与实现
查看>>
完美解决NC502手工sql的查询引擎排序及合计问题
查看>>