post 封装Map 发送请求
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<String, Object> propsMap) {
HttpResponse response = new HttpResponse();
String responseMsg = "";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);// POST请求
if (propsMap != null) {
// 参数设置
Set<String> 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<String, Object> propsMap) {
String responseMsg = "";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);// POST请求
// postMethod.
// 参数设置
Set<String> 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<String, Object> 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<String> 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<String, Object> 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<String> 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<String, Object> 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<String> 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;
}
}
post 封装Map 发送请求的更多相关文章
- Python 使用 requests 模块发送请求的使用及封装
一.requests 模块基本使用 1.准备接口的URL.请求参数.请求头 # 1. 构造注册.登录.充值请求的url register_url = "注册url" login_u ...
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
本人所使用软件 eclipse fiddle UC浏览器 分析请求信息 以知乎(https://www.zhihu.com)为例,模拟登陆请求,获取登陆后首页,首先就是分析请求信息. 用UC浏览器F1 ...
- http post发送请求
一: 用java自带URL发送 public synchronized JSONObject getJSON(String url2, String param) { try { URL url = ...
- consumer发送请求,接收响应
一般情况,consumer发送请求时,创建一个DefaultFuture对象,然后阻塞并等待响应.DefaultFuture类,封装了请求和响应: // 省略其他代码 public class Def ...
- 九、封装登录POST请求、登录后POST请求以及GET请求
一.封装登录后POST请求以及GET请求 /** * 全局运行时环境参数管理器 */ public static Map<String, String> BASE_GLOBAL_MAP; ...
- RestTemplate发送请求并携带header信息
1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...
- 封装的ajax请求
在做登录注册这类提交表单数据时,我们经常需要局部刷新网页来验证用户输入的信息,这就需要用到ajax请求,我们通常需要获取表单中的数据,发起ajax请求,通过服务程序,与数据库的数据进行比对,判断信息的 ...
- ajax-向服务器发送请求
ajax-向服务器发送请求 1.将请求发送到服务器,使用XMLHttpRequest对象的 open() 和 send() 方法. xmlhttp. open(method,url,async ...
- Android 网络请求库volley的封装,让请求更方便
首先封装一下volley 请求 public class CustomRequest extends StringRequest { private static final String TAG = ...
随机推荐
- ueditor的过滤、转义、格式丢失问题
1. 过滤 http://www.cnblogs.com/Olive116/p/3464495.html 2. 转义 http://segmentfault.com/q/101000000048928 ...
- Python学习 - 编写一个简单的web框架(一)
自己动手写一个web框架,因为我是菜鸟,对于python的一些内建函数不是清楚,所以在写这篇文章之前需要一些python和WSGI的预备知识,这是一系列文章.这一篇只实现了如何处理url. 参考这篇文 ...
- CentOS 6.4编译安装淘宝web服务器Tengine
Tengine 是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商城 ...
- List<T> ForEach break
有没有方法扩展跳出 list.foreach循环? 理论上它其实不是一个循环,而是一个方法 代理调用内部循环 public delegate void ForEachAction<T> ...
- hdu 5144 NPY and shot
http://acm.hdu.edu.cn/showproblem.php?pid=5144 题意:给你初始的高度和速度,然后让你求出水平的最远距离. 思路:三分枚举角度,然后根据公式求出水平距离. ...
- VS2010中fatal error LNK1123错误的解决方案
问题描述: 在VS2010项目编译时会出现如下错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 解决方案: 查找是否有两个cvtres.exe ...
- bzoj1430
这道题只是给bzoj1005做一个铺垫这里介绍了一个叫prufer编码的东西,就是给定一棵带标号的无根树,找出编号最小的叶子节点,写下与它相邻的节点的编号,然后删掉这个叶子节点.反复执行这个操作直到只 ...
- 服务器端打开office然后采用虚拟打印 转换成pdf
服务器端打开office然后采用虚拟打印 转换成pdf [WebMethod] public bool ConvertWordTOPDF(string WordPath) { bool ret=fal ...
- (转载)php数组添加、删除元素的方法
(转载)http://www.phpgs.com/html/php/phpjichu/20120130440.html 带来一篇php 数组 添加元素.删除元素的方法的文章,有需要的php学习者参考下 ...
- 201512读书分享——读《做自己的No.1》“哪一天”是永远不会到来的
在看这本书的同时,最近也看了一部电影叫做<令人怦然心动的人生整理魔法>,讲一个女孩子平常不善收理,然后房间和人生乱七八糟,她总想着“等哪一天有空了就收拾”.但是,最终一句话打醒了——“哪一 ...