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 = ...
随机推荐
- 知识库系统/知识管理系统 WCP
知识库系统/知识管理系统 WCP 本项目的应用场景是管理技术团队的相关知识(API.代码片段.知识定义.技术经验...) 但是其应用并不局限于这些应用,当然你最好下载一个安装版先试一试.其实这就是一个 ...
- 如何完美打造Win8 Metro版IE10浏览器页面(转)
Windows8 内置两种 Internet Explorer 10 (以下简称 IE10),一个是在桌面环境下使用的 IE10:视窗操作.可以支持各种插件(ActiveX):而另外一个则是在新的开始 ...
- 转:100个高质量Java开发者博客
原文来自于:http://www.importnew.com/7469.html ImportNew注:原文中还没有100个.作者希望大家一起来推荐高质量的Java开发博客,然后不段补充到这个列表.欢 ...
- 设计模式之观察者模式(Observer Pattern)
一.什么是观察者模式? 把现实世界中的报纸与订阅者的关系抽象出来就是观察者模式,一种报纸对应多个订阅者,订阅者可以随时解除订阅,未订阅的读者也可以随时开始订阅.一旦有新报纸发布,所有的订阅者都会收到新 ...
- 《how to design programs》15章 相互引用的数据定义
由结构体组成的表与结构体中的表. 在用追溯形式建立家家谱树时,我们通常从某个后代除法,依次处理它的父母,组父母等.而构建树时,我们会不断添加谁是谁的孩子,而不是写出谁是谁的父母,从而建立一颗后代家谱树 ...
- Unity 3D物体的点击事件响应以及NGUI坐标和世界坐标的互相转换
Unity 版本:4.5 NGUI版本:3.6.5 参考链接:http://game.ceeger.com/Script/Camera/Camera.ScreenPointToRay.html,Uni ...
- Going Home(最小费用最大流)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16200 Accepted: 8283 Description On a ...
- ural-1099-Work Scheduling(裸带花树)
题意: 有N个人,有限对的人可以在一起工作,问最多能有多少对. 分析: 任意图的最大匹配 // File MAXName: 1099.cpp // Author: Zlbing // Created ...
- 【数学相关、规律】Codeforces 696B Puzzles
题目链接: http://codeforces.com/problemset/problem/696/B 题目大意: 给一棵树,从根节点开始递归,time=1,每次递归等概率随机访问这个节点的子节点, ...
- One day
1.c的格式化输入输出: printf()和scanf()函数可以较好实现格式化输入输出,例子如下: printf("%3d",3); //结果为__3 (_为空格) pri ...