JAVA apache的HttpComponents项目的介绍和简单使用
1、HttpComponents项目的介绍
先介绍一下一个开源项目, 这个项目就是Apache开源组织中的HttpComponents,主要是提供对http服务器的访问功能,目前已经是一个单独的项目,可见http服务器的访问绝非易事。
在某些时候可能需要通过程序来访问这别人的网页程序,比如从别人的网页中“偷”一些数据。如果对方仅仅是一个很简单的页面,那我们的程序会很简单,。但是考虑到一些服务授权的问 题,很多公司提供的页面往往并不是可以通过一个简单的URL就可以访问的,而必须经过注册然后登录后方可使用提供服务的页面,这个时候就涉及到 COOKIE问题的处理。我们知道目前流行的动态网页技术例如ASP、JSP无不是通过COOKIE来处理会话信息的。为了使我们的程序能使用别人所提供的服务页面,就要求程序首先登录后再访问服务页面!再比如通过HTTP来上传文件呢?
HttpComponents项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程。通过它可以让原来很头疼的事情现在轻松的解决,例如你不再管是HTTP或者HTTPS的通讯方式,告诉它你想使用HTTPS方式,剩下的事情交给 httpclient替你完成。
网址:http://www.apache.org/ 主页下面的Projects 可以找到HttpComponents项目,下载到相应的jar包,以及源代码跟示例程序。
2、httpcomponents 4.5.2 简单使用
HttpComponents 也就是以前的httpclient项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端/服务器编程工具包,并且它支持 HTTP 协议最新的版本和建议。不过现在的 HttpComponents 包含多个子项目,有:
HttpComponents Core
HttpComponents Client
HttpComponents AsyncClient
Commons HttpClient
以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的主页。
实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
支持自动转向
支持 HTTPS 协议
支持代理服务器等
支持Cookie
之前用的HttpClient的方法都失效了,比如 httpclinet =new DefaultHttpClient(); 不知道为啥包里80%的类都失效了,总是觉得创建一个httpclient实例会比较麻烦,记不住。所以整理了一下新的方式简单使用。
简单使用
导入jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
get请求获取响应
CloseableHttpClient httpClient= HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://www.baidu.com");
CloseableHttpResponse response = httpClient.execute(httpget);
HttpEntity httpEntity= response.getEntity();
String strResult= EntityUtils.toString(httpEntity);
post请求获取响应
HttpPost httpost = new HttpPost("https://www.baidu.com");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", "username"));
nvps.add(new BasicNameValuePair("IDToken2", "password"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
HttpClient提供URIBuilder工具类来简化URIs的创建和修改过程。
URI uri = new URIBuilder()
.setScheme("http")
.setHost("www.google.com")
.setPath("/search")
.setParameter("q", "httpclient")
.setParameter("btnG", "Google Search")
.setParameter("aq", "f")
.setParameter("oq", "")
.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI())
模拟浏览器请求
httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
3、HttpComponents之httpclient基本使用方法 - httpclient的版本是4.3
A、第一种方式
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HttpClient2 {
public static void main(String args[]) throws IOException {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("account", ""));
formparams.add(new BasicNameValuePair("password", ""));
HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://cnivi.com.cn/login");
post.setEntity(reqEntity);
post.setConfig(requestConfig);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
String message = EntityUtils.toString(resEntity, "utf-8");
System.out.println(message);
} else {
System.out.println("请求失败");
}
}
}
B、第二种方式,这种方式是用了一个http的连接池,同时使用httpbuilder构造合适的http方法。
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.*;
public class HttpClientUtils {
private static PoolingHttpClientConnectionManager connectionManager = null;
private static HttpClientBuilder httpBulder = null;
private static RequestConfig requestConfig = null;
private static int MAXCONNECTION = 10;
private static int DEFAULTMAXCONNECTION = 5;
private static String IP = "cnivi.com.cn";
private static int PORT = 80;
static {
//设置http的状态参数
requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
HttpHost target = new HttpHost(IP, PORT);
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(MAXCONNECTION);
connectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION);
connectionManager.setMaxPerRoute(new HttpRoute(target), 20);
httpBulder = HttpClients.custom();
httpBulder.setConnectionManager(connectionManager);
}
public static CloseableHttpClient getConnection() {
CloseableHttpClient httpClient = httpBulder.build();
httpClient = httpBulder.build();
return httpClient;
}
public static HttpUriRequest getRequestMethod(Map<String, String> map, String url, String method) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> e : entrySet) {
String name = e.getKey();
String value = e.getValue();
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
HttpUriRequest reqMethod = null;
if ("post".equals(method)) {
reqMethod = RequestBuilder.post().setUri(url)
.addParameters(params.toArray(new BasicNameValuePair[params.size()]))
.setConfig(requestConfig).build();
} else if ("get".equals(method)) {
reqMethod = RequestBuilder.get().setUri(url)
.addParameters(params.toArray(new BasicNameValuePair[params.size()]))
.setConfig(requestConfig).build();
}
return reqMethod;
}
public static void main(String args[]) throws IOException {
Map<String, String> map = new HashMap<String, String>();
map.put("account", "");
map.put("password", "");
HttpClient client = getConnection();
HttpUriRequest post = getRequestMethod(map, "http://cnivi.com.cn/login", "post");
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String message = EntityUtils.toString(entity, "utf-8");
System.out.println(message);
} else {
System.out.println("请求失败");
}
}
}
=============END=============
以上内容为转载,只是把三个文章的内容整在了一起
---------------------
作者:单人_旅途
来源:CSDN
原文:https://blog.csdn.net/wjrong_1/article/details/51114233
版权声明:本文为博主原创文章,转载请附上博文链接!