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 = ...
随机推荐
- 解读为什么有符号的char可表示范围是-128~+127
问:为什么有符号的char可表示范围是-128~+127? 要明白这个问题,首先要明白一下几点: 对于char和int计算机中以补码形式存在. 严格来说计算机就是傻逼,它只知道某个位上是0还是1. 我 ...
- 关于C#匿名方法
作者 陈嘉栋(慕容小匹夫) 阅读目录 0x00 前言 0x01 不必构造委托对象 0x02 匿名方法初探 0x03 使用匿名方法省略参数 0x04 匿名方法和闭包 0x05 匿名方法如何捕获外部变量 ...
- oracle 对象权限 系统权限 角色权限
系统权限: 允许用户执行特定的数据库动作,如创建表.创建索引.连接实例等 对象权限: 允许用户操纵一些特定的对象,如读取视图,可更新某些列.执行存储过程等 select * from user_sys ...
- 看奢侈品Prada如何使用物联网
这是峰哥在一家国际顶级商学院听课的笔记.这是个巨变的时代,有趣的时代. 一 PRADA在纽约的旗舰店.每件衣服上都有RFID码.每当一个顾客拿起一件PRADA进试衣间,RFID会被自动识别,试衣间里的 ...
- 4. Repeater 实例2
设计管理一个用户程序,对用户的状态进行管理,当用户状态是启用时整行显示红色. 设计思路:用Repeater遍历每行记录,在操作状态的表格中旋转两个按钮,一个为启用功能,另一个为禁用功能,根据Repea ...
- Maven的常用命令
转载:http://www.cnblogs.com/phoebus0501/archive/2011/05/10/2042511.html Maven库: http://repo2.maven.org ...
- linux setsockopt
linux setsockopt Socket描述符选项[SOL_SOCKET] #include <sys/socket.h> int setsockopt( int socket, ...
- AlgorithmsI Exercises: Analysis of Algorithms
Question 1 Suppose that you time a program as a function of N and producethe following table. N seco ...
- 【转】 Android开发之EditText属性详解
原文网址:http://blog.csdn.net/qq435757399/article/details/7947862 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: ...
- (转载)PHP 提示和技巧
(转载)http://www.111cn.net/phper/21/b4aea31507014a778b18682943db402f.htm 1. 当您在寻找关于某个具体的 PHP 函数的信息时,请转 ...