package com.xing.weix.util;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.*;
import java.util.Map.Entry;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
/***
* *
* 类名称: HttpService.java
* 类描述: http 请求工具类,莫有那么多什么异常什么异常,分分钟搞定的事情,搞那么复杂
* 创建人:
* 创建时间: 2016-11-16下午5:14:37
* 修改人: liuxing
* 修改时间: 2016-11-16下午5:14:37
* 修改备注: API Code:http://hc.apache.org/httpcomponents-core-4.3.x/examples.html
* @version
*/
public class HttpReqUtil extends AsyncHttp {
protected static Logger logger = Logger.getLogger(HttpReqUtil.class);
private static String userAgent = "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";
private HttpReqUtil() {}
/**
* post 请求直接请求
* @param url 请求地址
* @param param 请求参数,字符串
* @return 相应结果
* @throws
*/
public static HttpResponse postReq(String url, String param) throws Exception {
HttpPost post = new HttpPost(url);
HttpResponse httpResponse = null ;
try {
// post.setConfig(requestConfig); // 父类定义的超时信息,在每个请求都要置入配置参数
post.setEntity(new StringEntity(param, ContentType.create(
"application/x-www-form-urlencoded", Consts.UTF_8)));
httpResponse = createHttpClient().execute(post); // 将post请求交给HttpClient请求去执行
} catch (IOException e) {
return null ;
}
return httpResponse;
}
/**
* get请求
* @return
*/
public static String doGet(String url) {
try {
HttpClient client = createHttpClient();
//发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/**请求发送成功,并得到响应**/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/**读取服务器返回过来的json字符串数据**/
String strResult = EntityUtils.toString(response.getEntity());
return strResult;
}
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* post 对象请求
* @param httpUri 请求地址
* @param param 参数对象
* @return 请求结果
* @throws
*/
public static HttpResponse postObjectReq(String httpUri, Object param)throws Exception {
HttpPost post = new HttpPost( httpUri );
String result = "";
HttpResponse httpResponse = null ;
try {
// post.setConfig(requestConfig); // 父类定义的超时信息,在每个请求都要置入配置参数
post.setHeader("User-Agent",userAgent); //需要加这个参数,否则返回403 状态吗
post.addHeader("Content-type", "application/json; charset=utf-8"); //设置请求头部
post.setHeader("Accept", "application/json");
/* post.addHeader("Content-type", "application/text");
post.addHeader("Charset", "UTF-8"); */
ObjectMapper objectMapper = new ObjectMapper();
String josnParm = objectMapper.writeValueAsString(param);
//String josnParm = JSONObject.toJSONString(param).toString(); //将请求对象由java对象置为json行字符对象。因为请求内设定了请求数据类型为json类型
post.setEntity(new StringEntity(josnParm));
httpResponse = createHttpClient().execute(post); //将post请求交给HttpClient 请求去执行
} catch (IOException e) {
return null ;
}
return httpResponse ;
}
/**
* post 对象请求
* @param httpUri 请求地址
* @param param 参数对象
* @return 请求结果
* @throws
*/
public static HttpResponse postObjectReqForm(String httpUri, Object param)throws Exception {
HttpPost post = new HttpPost( httpUri );
HttpResponse httpResponse = null ;
try {
// post.setConfig(requestConfig); // 父类定义的超时信息,在每个请求都要置入配置参数
post.setHeader("User-Agent",userAgent); //需要加这个参数,否则返回403 状态吗
post.addHeader("Content-type", "application/form-data; charset=UTF-8"); //设置请求头部
post.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
ObjectMapper objectMapper = new ObjectMapper();
String josnParm = objectMapper.writeValueAsString(param);
//String josnParm = JSONObject.toJSONString(param).toString(); //将请求对象由java对象置为json行字符对象。因为请求内设定了请求数据类型为json类型
josnParm = josnParm.substring(1, josnParm.length()) ;
josnParm = josnParm.substring(0, josnParm.length()-1) ;
josnParm = josnParm.replaceAll(",", "/n");
post.setEntity(new StringEntity(josnParm));
httpResponse = createHttpClient().execute(post); //将post请求交给HttpClient 请求去执行
} catch (IOException e) {
return null ;
}
return httpResponse ;
}
/**
* 模拟请求
*
* @param httpUri 资源地址
* @param param 参数列表
* @return
* @throws ParseException
* @throws IOException
*/
public static HttpResponse postSendReqForm( String httpUri,Object param ) throws Exception{
HttpPost httpPost = new HttpPost( httpUri );
HttpResponse httpResponse = null ;
try {
//将请求对象由java对象置为json行字符对象。因为请求内设定了请求数据类型为json类型
ObjectMapper objectMapper = new ObjectMapper();
String josnParm = objectMapper.writeValueAsString(param);
//String josnParm = JSONObject.toJSONString(param).toString();
@SuppressWarnings("unchecked")
Map<String,String> map = objectMapper.readValue( josnParm, Map.class ); //json转换成map
//Map<String,String> map = (Map<String,String>) JSON.parseObject( josnParm , Map.class ) ;
//装填参数 ---不同的协议传输的数据不一样,很坑爹的,所以大家一定要定义好
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(map!=null){
for (Entry<String, String> entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
System.out.println("请求地址:"+httpUri );
logger.info("请求参数:"+nvps.toString());
//设置header信息
//指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("User-Agent",userAgent); //需要加这个参数,否则返回403 状态吗
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
//httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
httpResponse = createHttpClient().execu
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
weixinLogin.rar (42个子文件)
weixinLogin
pom.xml 2KB
target
test-classes
com
xing
weix
WeixinLoginApplicationTests.class 637B
classes
META-INF
maven
com.xing
weixinLogin
pom.xml 2KB
pom.properties 222B
MANIFEST.MF 345B
application.properties 79B
com
xing
weix
auth
WeixinUtil.class 4KB
domain
MemberChannel.class 2KB
confi
SendNote.class 762B
controller
WeixinBindController.class 5KB
WeixinAuthController.class 7KB
util
Order.class 713B
HttpReqUtil.class 10KB
AsyncHttp.class 3KB
HttpClientConnectionMonitorThread.class 2KB
WeixinLoginApplication.class 719B
mvnw 6KB
.settings
org.eclipse.m2e.core.prefs 97B
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.core.resources.prefs 155B
org.eclipse.jdt.core.prefs 243B
src
test
java
com
xing
weix
WeixinLoginApplicationTests.java 335B
main
resources
application.properties 79B
templates
static
java
com
xing
weix
auth
WeixinUtil.java 6KB
domain
MemberChannel.java 1KB
confi
SendNote.java 706B
WeixinLoginApplication.java 315B
controller
WeixinAuthController.java 10KB
WeixinBindController.java 7KB
util
HttpClientConnectionMonitorThread.java 1KB
AsyncHttp.java 5KB
HttpReqUtil.java 14KB
.project 1KB
使用说明
1.jpg 51KB
2.png 49KB
4.png 34KB
3.png 58KB
.classpath 1KB
.gitignore 268B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
共 42 条
- 1
资源评论
悟能的师兄
- 粉丝: 877
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功