HttpClient实现的工具类

就是簡單的用http 協議請求請求地址並返回數據,廢話少數直接上代碼

http請求返回的封裝類

    package com.nnk.upstream.util;

    import org.apache.http.cookie.Cookie;

    import java.util.HashMap;
import java.util.List; /**
* http请求返回结果封装
*/
public class HttpResponse {
//返回的數據
private String content=null;
//返回的cookies
private HashMap<String, String> cookie=null;
//重定向的地址
private String location=null;
//記錄時間
private String date;
//返回的狀態
private int status;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public HashMap<String, String> getCookie() {
return cookie;
}
public void setCookie(HashMap<String, String> cookie) {
this.cookie = cookie;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
} public int getStatus() {
return status;
} public void setStatus(int status) {
this.status = status;
} private List<Cookie> cookieList; public List<Cookie> getCookieList() {
return cookieList;
} public void setCookieList(List<Cookie> cookieList) {
this.cookieList = cookieList;
}
}

httpUtils 工具類

    package com.nnk.upstream.util;

    import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger; /**
* Http连接工具类
*/
public class HttpUtil { private static Logger log=Logger.getLogger(HttpUtil.class);
public static RequestConfig getDefConf() {
return RequestConfig.custom() //设置连接超时
.setSocketTimeout(60000)
.setConnectTimeout(60000)
.setConnectionRequestTimeout(50000)
.build();
}
/**
* 发送get请求并接收返回结果
* @param url 请求的地址
* @param charset 编码方式
* @return response的bean实例
*/
public static HttpResponse get(String url,HashMap<String, String> cookie,String charset){
CloseableHttpClient httpClient=HttpClients.custom()
.setDefaultRequestConfig(getDefConf())
.build();;
HttpResponse httpResponse=new HttpResponse();
try{
HttpGet httpGet=new HttpGet(url);
StringBuilder reqCookieStr=new StringBuilder();
Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
while (iterator.hasNext()){
Entry<String, String> entry=iterator.next();
reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
}
httpGet.addHeader("Cookie",reqCookieStr.toString());
httpGet.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
httpGet.addHeader("Connection","Keep-Alive");
httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
httpGet.addHeader("Accept-Encoding","gzip, deflate");
httpGet.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//以下header要留着
httpGet.addHeader("Referer","http://wap.10010.com/t/home.htm");
CloseableHttpResponse response=httpClient.execute(httpGet);
HttpEntity entity=response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
log.info("status:"+response.getStatusLine());
httpResponse.setStatus(statusCode);
if(statusCode==200||statusCode==302){
Header[] responseHeaders=response.getAllHeaders();
for(Header header:responseHeaders){
if(header.getName().equals("Set-Cookie")){
String[] jseeiond = header.getValue().split(";")[0].split("=");
cookie.put(jseeiond[0],jseeiond[1]);
}else if(header.getName().equals("Location")){
httpResponse.setLocation(header.getValue());
}
}
httpResponse.setContent(EntityUtils.toString(entity,charset));
httpResponse.setCookie(cookie);
}else{
log.error("访问Url异常:"+url);
}
try{
response.close();
}catch (Exception e) {
log.error(e);
}
}catch(Exception e){
e.printStackTrace();
}
return httpResponse;
} /**
* 发送post请求并接收返回结果
* @param url 请求的地址
* @param cookie 请求的cookie
* @param nvps post请求的参数
* @param responseCharSet 返回内容的编码方式
* @return 发送信息的处理结果
*/
public static HttpResponse post(String url,HashMap<String, String> cookie,List<NameValuePair> nvps,String responseCharSet){
CloseableHttpClient httpClient=HttpClients.custom()
.setDefaultRequestConfig(getDefConf())
.build();;
HttpResponse httpResponse=new HttpResponse();
try{
HttpPost httpPost=new HttpPost(url);
StringBuilder reqCookieStr=new StringBuilder();
Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
while (iterator.hasNext()){
Entry<String, String> entry=iterator.next();
reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
}
httpPost.addHeader("Cookie",reqCookieStr.toString());
httpPost.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
httpPost.addHeader("Connection","Keep-Alive");
httpPost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
httpPost.addHeader("Accept-Encoding","gzip, deflate");
httpPost.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//以下header要留着
httpPost.addHeader("Referer","http://wap.10010.com/t/home.htm");
httpPost.setEntity(new UrlEncodedFormEntity(nvps,responseCharSet));
log.info("post datas:" + EntityUtils.toString(httpPost.getEntity()));
CloseableHttpResponse response=httpClient.execute(httpPost); try{
HttpEntity entity=response.getEntity();
Header[] responseHeaders=response.getAllHeaders();
int statusCode = response.getStatusLine().getStatusCode();
log.info("status:" + response.getStatusLine());
httpResponse.setStatus(statusCode);
for (Header header : responseHeaders) {
if (header.getName().equals("Set-Cookie")) {
Matcher matcher = Pattern.compile("(.+?)=(.*?);\\s{0,1}Domain").matcher(header.getValue());
while (matcher.find()) {
cookie.put(matcher.group(1), matcher.group(2));
}
} else if (header.getName().equals("Location")) {
httpResponse.setLocation(header.getValue());
}
} httpResponse.setContent(EntityUtils.toString(entity, responseCharSet));
httpResponse.setCookie(cookie);
}finally{
response.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
return httpResponse;
} /**
* 发送post请求并接收返回结果
* @param url 请求的地址
* @param cookie 请求的cookie
* @param nvps post请求的参数
* @return 发送信息的处理结果
*/
public static HttpResponse downLoad(String url,HashMap<String, String> cookie,List<NameValuePair> nvps,File file){
CloseableHttpClient httpClient=HttpClients.custom()
.setDefaultRequestConfig(getDefConf())
.build();;
HttpResponse httpResponse=new HttpResponse();
try{
HttpPost httpPost=new HttpPost(url);
StringBuilder reqCookieStr=new StringBuilder();
Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
while (iterator.hasNext()){
Entry<String, String> entry=iterator.next();
reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
}
httpPost.addHeader("Cookie",reqCookieStr.toString());
httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"GB2312"));
CloseableHttpResponse response=httpClient.execute(httpPost);
try{
HttpEntity entity=response.getEntity();
response.getStatusLine().getStatusCode();
Header[] responseHeaders=response.getAllHeaders();
for(Header header:responseHeaders){
if(header.getName().equals("Set-Cookie")){
Matcher matcher=Pattern.compile("(.+?)=(.*?);\\s{0,1}Domain").matcher(header.getValue());
while(matcher.find()){
cookie.put(matcher.group(1),matcher.group(2));
}
}else if(header.getName().equals("Location")){
httpResponse.setLocation(header.getValue());
}
}
FileOutputStream fos=new FileOutputStream(file);
fos.write(EntityUtils.toByteArray(entity));
fos.close();
httpResponse.setCookie(cookie);
}finally{
response.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
return httpResponse;
} public static String htmlEscape(String content){
content=content.replace("&amp;","&").replace("%","%").replace("&lt;","<").replace("&gt;",">");
return content;
} public static HttpResponse sendGet(String url,Cookie[] cookies,String charset) {
HttpResponse httpResponse = new HttpResponse();
//1.获得一个httpclient对象
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultRequestConfig(getDefConf())
.build(); CloseableHttpResponse response = null;
try {
// 设置cookies
BasicCookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookies(cookies); HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); //2.生成一个get请求
HttpGet httpget = new HttpGet(url);
httpget.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
httpget.addHeader("Connection","Keep-Alive");
httpget.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
httpget.addHeader("Accept-Encoding","gzip, deflate");
httpget.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//以下header要留着
httpget.addHeader("Referer","http://wap.10010.com/t/home.htm");
BasicCookieStore cookieStore2;
//3.执行get请求并返回结果
response = httpclient.execute(httpget, httpContext);
cookieStore2 = (BasicCookieStore)httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
//返回内容设置
httpResponse.setContent(EntityUtils.toString(response.getEntity(),charset));
int stauts = response.getStatusLine().getStatusCode();
//返回状态设置
httpResponse.setStatus(stauts);
//设置返回的Cookie
List<Cookie> cookies1 = cookieStore2.getCookies();
cookies1.addAll(Arrays.asList(cookies));
httpResponse.setCookieList(cookies1);
Header[] responseHeaders=response.getAllHeaders();
for(Header header:responseHeaders){
if(header.getName().equals("Location")){
httpResponse.setLocation(header.getValue());
}
}
} catch (Exception e1) {
e1.printStackTrace();
log.info("请求异常.....");
}finally {
try {
if(null != response) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return httpResponse;
} public static HttpResponse sendPost(String url,Cookie[] cookies,List<NameValuePair> nvps ,String charset) {
HttpResponse httpResponse = new HttpResponse();
//1.获得一个httpclient对象
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultRequestConfig(getDefConf())
.build(); CloseableHttpResponse response = null;
BasicCookieStore cookieStore2;
try {
// 设置cookies
BasicCookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookies(cookies);
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); //2.生成一个get请求
HttpPost httppost = new HttpPost(url);
httppost.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
httppost.addHeader("Connection","Keep-Alive");
httppost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
httppost.addHeader("Accept-Encoding","gzip, deflate");
httppost.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//以下header要留着
httppost.addHeader("Referer","http://wap.10010.com/t/home.htm");
HttpEntity entity = new UrlEncodedFormEntity(nvps,charset);
log.info("post data:"+EntityUtils.toString(entity));
httppost.setEntity(entity);
//3.执行get请求并返回结果
response = httpclient.execute(httppost, httpContext);
cookieStore2 = (BasicCookieStore)httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
//返回内容设置
httpResponse.setContent(EntityUtils.toString(response.getEntity(),charset));
int stauts = response.getStatusLine().getStatusCode();
//返回状态设置
httpResponse.setStatus(stauts);
//设置返回的Cookie
List<Cookie> cookies1 = cookieStore2.getCookies();
cookies1.addAll(Arrays.asList(cookies));
httpResponse.setCookieList(cookies1);
Header[] responseHeaders=response.getAllHeaders();
for(Header header:responseHeaders){
if(header.getName().equals("Location")){
httpResponse.setLocation(header.getValue());
}
}
} catch (Exception e1) {
e1.printStackTrace();
log.info("请求异常.....");
}finally {
try {
if(null != response) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return httpResponse;
}
}

封装重定向的HttpProxyUtils

    package com.nnk.upstream.util;
import com.nnk.interfacetemplate.common.StringUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.NameValuePair;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.log4j.Logger; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*; /**
* Created with IntelliJ IDEA.
* User: xxydl
* Date: 2016/11/11
* Time: 19:12
* email: xxydliuy@163.com
* To change this template use File | Settings | File Templates.
*/
public class HttpProxyUtil {
public static final Logger log = Logger.getLogger(HttpProxyUtil.class);
public static HttpResponse get(String url,List<org.apache.http.cookie.Cookie> cookielist,String charset){
boolean ret = true;
HttpResponse response = null;
do{
if(StringUtil.isEmpty(url)){
log.warn("url is Empty");
break;
}else{
log.info("get url:" + url);
org.apache.http.cookie.Cookie[] arr = (org.apache.http.cookie.Cookie[])cookielist.toArray(new org.apache.http.cookie.Cookie[cookielist.size()]);
response = HttpUtil.sendGet(url, arr, charset);
}
if(response!=null && 200==response.getStatus()){
return response;
}else if(response!=null && 302==response.getStatus()){
url = response.getLocation();
cookielist = response.getCookieList();
log.info("重定向到Url:" + url);
log.info("cookies:" + response.getCookieList());
}else {
return response;
}
}while (true);
return response;
} public static HttpResponse post(String url,List<org.apache.http.cookie.Cookie> cookielist,List<NameValuePair> nvps,String charset){
boolean isPost = true;
HttpResponse response = null;
do{
org.apache.http.cookie.Cookie[] arr = (org.apache.http.cookie.Cookie[])cookielist.toArray(new org.apache.http.cookie.Cookie[cookielist.size()]);
if(StringUtil.isEmpty(url)){
log.warn("url is Empty");
break;
}else if(isPost){
log.info("post url:" + url); response = HttpUtil.sendPost(url, arr, nvps, charset);
}else {
response = HttpUtil.sendGet(url, arr, charset);
}
if(response!=null && 200==response.getStatus()){
return response;
}else if(response!=null && 302==response.getStatus()){
url = response.getLocation();
log.info("重定向到Url:" + url);
log.info("cookies:" + response.getCookieList());
cookielist = response.getCookieList();
isPost = false;
}else {
return response;
}
}while (true);
return response;
}

以上有些方法可能没有自己简单实现就可以了。

httpclient 实现的http工具类的更多相关文章

  1. HttpClient 4.5.x 工具类设计与实现

    最近,业务需要在java服务端发起http请求,需要实现"GET","POST","PUT"等基本方法.于是想以 "HttpCli ...

  2. 使用HttpClient访问url的工具类

    maven依赖jar包配置: <dependency> <groupId>org.apache.httpcomponents</groupId> <artif ...

  3. Httpclient 工具类(get,put)

    package com.googosoft.until; import java.io.IOException; import org.apache.http.HttpEntity; import o ...

  4. HttpUtil工具类,发送Get/Post请求,支持Http和Https协议

    HttpUtil工具类,发送Get/Post请求,支持Http和Https协议 使用用Httpclient封装的HttpUtil工具类,发送Get/Post请求 1. maven引入httpclien ...

  5. 使用单例模式实现自己的HttpClient工具类

    引子 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient 来方便我们使用各种Http服务.你可以把HttpCli ...

  6. 基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类

    本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.因为本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建议 ...

  7. Android开发实现HttpClient工具类

    在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.你可以把HttpClient想 ...

  8. http和https工具类 (要注意httpclient版本号和log4j的版本号)

    1 工具类 package dd.com; import java.io.IOException; import java.security.cert.CertificateException; im ...

  9. 170314、工具:apache httpClient多线程并发情况下安全实用及工具类分享

    简单用法介绍:介绍来源网络 建立连接:在HttpClient中使用多线程的一个主要原因是可以一次执行多个方法.在执行期间,每一个方法都使用一个HttpConnection实例.由于在同一时间多个连接只 ...

随机推荐

  1. go语言20小时从入门到精通(六、工程管理)

    在实际的开发工作中,直接调用编译器进行编译和链接的场景是少而又少,因为在工程中不会简单到只有一个源代码文件,且源文件之间会有相互的依赖关系.如果这样一个文件一个文件逐步编译,那不亚于一场灾难. Go语 ...

  2. <转>常规测试方法

    功能测试 1. 安装测试: 安装过程中对于缺省安装目录及任意指定的安装目录,是否都能正确安装: 若是选择安装,查看能否实现其相应的功能: 在所有能中途退出安装的位置退出安装程序后,验证此程序并未安装成 ...

  3. Django 字段更新时报错

    字段更新时会报错: -------------------------------------------------------------- 无法向未定义字段添加默认值您确认添加默认值吗? 请添加 ...

  4. 【AMAD】beaker -- 用于session和缓存的WSGI中间件

    简介 动机 作用 个人评分 简介 Beaker1是一个web session和通用缓存库,并且包含一个WSGI中间件可以用于你的web应用. 动机 Beaker是基于MyghtyUtils2(一个古老 ...

  5. AKKA文档2.1(java版)——什么是AKKA?

    可扩展的实时事务处理 我们相信编写并发.容错.可扩展的应用相当的困难.盖因大多数时候我们一直在使用错误的工具和错误的抽象等级.AKKA就是为了改变这一切的.我们利用角色模型提升了抽象等级,并且提供了一 ...

  6. Laravel 查询&数据库&模型

    1.with()与load区别: 都称为 延迟预加载,不同点在于 load()是在已经查询出来的模型上调用,而 with() 则是在 ORM 查询构造器上调用. Order::query()-> ...

  7. mac环境提示:make sure that /usr/local/bin is in your path

    今天我在Mac环境下安装了Homebrew和node.js后,输入node -v.brew.npm都提示: command not found,然后我查看了下Homebrew的安装日志,发现日志里有个 ...

  8. PDF技术(四)-Java实现Html转PDF文件

    html转换为pdf的关键技术是如何处理网页中复杂的css样式.以及中文乱码处理. 各实现对比表于Windows平台进行测试: 基于IText 基于FlyingSaucer 基于WKHtmlToPdf ...

  9. Oracle密码过期(the password has expired)

    1.进入sqlplus模式--sqlplus / as sysdba; 2.查看用户密码的有效期设置(一般默认的配置文件是DEFAULT) SELECT * FROM dba_profiles WHE ...

  10. [BJOI2014]大融合(Link Cut Tree)

    [BJOI2014]大融合(Link Cut Tree) 题面 给出一棵树,动态加边,动态查询通过每条边的简单路径数量. 分析 通过每条边的简单路径数量显然等于边两侧节点x,y子树大小的乘积. 我们知 ...