httpclient 实现的http工具类
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("&","&").replace("%","%").replace("<","<").replace(">",">");
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工具类的更多相关文章
- HttpClient 4.5.x 工具类设计与实现
最近,业务需要在java服务端发起http请求,需要实现"GET","POST","PUT"等基本方法.于是想以 "HttpCli ...
- 使用HttpClient访问url的工具类
maven依赖jar包配置: <dependency> <groupId>org.apache.httpcomponents</groupId> <artif ...
- Httpclient 工具类(get,put)
package com.googosoft.until; import java.io.IOException; import org.apache.http.HttpEntity; import o ...
- HttpUtil工具类,发送Get/Post请求,支持Http和Https协议
HttpUtil工具类,发送Get/Post请求,支持Http和Https协议 使用用Httpclient封装的HttpUtil工具类,发送Get/Post请求 1. maven引入httpclien ...
- 使用单例模式实现自己的HttpClient工具类
引子 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient 来方便我们使用各种Http服务.你可以把HttpCli ...
- 基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类
本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.因为本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建议 ...
- Android开发实现HttpClient工具类
在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.你可以把HttpClient想 ...
- http和https工具类 (要注意httpclient版本号和log4j的版本号)
1 工具类 package dd.com; import java.io.IOException; import java.security.cert.CertificateException; im ...
- 170314、工具:apache httpClient多线程并发情况下安全实用及工具类分享
简单用法介绍:介绍来源网络 建立连接:在HttpClient中使用多线程的一个主要原因是可以一次执行多个方法.在执行期间,每一个方法都使用一个HttpConnection实例.由于在同一时间多个连接只 ...
随机推荐
- 七十一:flask钩子函数之关于context_processor的钩子函数
context_processor:使用这个钩子函数,必须返回一个字典,这个字典的值在所有模板中都可以使用,这个钩子函数作用是,如果一些在很多模板中都要用到的变量,那么就可以使用此钩子函数来返回,而不 ...
- 五十一:数据库之Flask-Migrate详解
在实际开发中,经常会发生数据库修改行为,一般数据库修改不是直接手动修改,而是去修改ORM模型,然后再把模型映射到数据库中,这些操作可以通过flask-migrate实现,flask-migrate是基 ...
- CSV Data Set Config参数说明
以下是CSV Data Set Config各个参数的简要说明: FileName:即同目录下csv文件的名称 File Encoding: 默认为ANSI Varible Names: 定义文本文件 ...
- 【转载】WEBRTC基本介绍
“WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的技术,是谷歌2010年以6820万美元收购Glob ...
- 2019.11.18【每天学点SAP小知识】Day4 - ABAP 7.40新语法 FOR
"今天学习一下FOR的语法,常用的2个语法. FOR wa|<fs> IN itab [INDEX INTO idx] [cond] "FOR i = … [THEN ...
- tcpdump分析tcp连接的建立、传输和关闭
http://note.youdao.com/noteshare?id=2aa0379d1e4b3bcddc26174861ffe09a
- 重启 hdfs and yarn datanode
1- start-all.sh and stop-all.sh: Used to start and stop hadoop daemons all at once. Issuing it on th ...
- body和document的梗
http://bbs.zhinengshe.com/thread-1199-1-1.html 1. 在空白的页面加点击事件,是加在body上么 ? <!DOCTYPE html> < ...
- python多媒体文件抽取
多文件抽取有:只获取url,或直接下载,下面是怎么将数据下载下来,并显示进度. 本节主要介绍urllib模块提供的urlretrieve()函数.urlretrieve()方法直接将远程数据下载到本地 ...
- 分布式架构-Redis 从入门到精通 完整案例 附源码
导读 篇幅较长,干货十足,阅读需要花点时间,全部手打出来的字,难免出现错别字,敬请谅解.珍惜原创,转载请注明出处,谢谢~! NoSql介绍与Redis介绍 什么是Redis? Redis是用C语言开发 ...