https://blog.csdn.net/irokay/article/details/78801307html
HTTP 协议多是如今 Internet 上使用得最多、最重要的协议了,愈来愈多的 Java 应用程序须要直接经过 HTTP 协议来访问网络资源。虽然在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能,可是对于大部分应用程序来讲,JDK 库自己提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,而且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在不少的项目中,好比 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。更多信息请关注http://hc.apache.org/java
许多须要后台模拟请求的系统或者框架都用的是httpclient,使用HttpClient发送请求、接收响应很简单,通常须要以下几步便可:apache
先看个官方HttpClient经过Http协议发送get请求,请求网页内容的例子:编程
1.ClientWithResponseHandler.javaapi
/* * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.http.examples.client; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; /** * This example demonstrates the use of the {@link ResponseHandler} to simplify * the process of processing the HTTP response and releasing associated resources. */ public class ClientWithResponseHandler { public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet("http://www.baidu.com/"); System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse( final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } finally { httpclient.close(); } } }
我把上述例子中的请求地址改成了“http://www.baidu.com/”,运行后控制台能够获取百度首页网页内容:服务器
下面把地址改成https地址:https://www.baidu.com/,再次尝试运行:
报错了,提示unable to find valid certification path to requested target,没法经过htpps认证。网络
正规途径,咱们须要将证书导入到密钥库中,如今咱们采起另一种方式:绕过https证书认证明现访问。app
2.Method SSLContext框架
/** * 绕过验证 * * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sc = SSLContext.getInstance("SSLv3"); // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法 X509TrustManager trustManager = new X509TrustManager() { @Override public void checkClientTrusted( java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public void checkServerTrusted( java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } }; sc.init(null, new TrustManager[] { trustManager }, null); return sc; }
修改1中main方法:socket
public final static void main(String[] args) throws Exception { String body = ""; //采用绕过验证的方式处理https请求 SSLContext sslcontext = createIgnoreVerifySSL(); //设置协议http和https对应的处理socket连接工厂的对象 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); HttpClients.custom().setConnectionManager(connManager); //建立自定义的httpclient对象 CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build(); //CloseableHttpClient client = HttpClients.createDefault(); try{ //建立get方式请求对象 HttpGet get = new HttpGet("https://www.baidu.com/"); //指定报文头Content-type、User-Agent get.setHeader("Content-type", "application/x-www-form-urlencoded"); get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"); //执行请求操做,并拿到结果(同步阻塞) CloseableHttpResponse response = client.execute(get); //获取结果实体 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity, "UTF-8"); } EntityUtils.consume(entity); //释放连接 response.close(); System.out.println("body:" + body); } finally{ client.close(); } }
运行代码,获取网页内容成功!
同理,再尝试下post请求:
public final static void main(String[] args) throws Exception { String body = ""; //采用绕过验证的方式处理https请求 SSLContext sslcontext = createIgnoreVerifySSL(); //设置协议http和https对应的处理socket连接工厂的对象 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); HttpClients.custom().setConnectionManager(connManager); //建立自定义的httpclient对象 CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build(); //CloseableHttpClient client = HttpClients.createDefault(); try{ //建立post方式请求对象 HttpPost httpPost = new HttpPost("https://api.douban.com/v2/book/1220562"); //指定报文头Content-type、User-Agent httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"); //执行请求操做,并拿到结果(同步阻塞) CloseableHttpResponse response = client.execute(httpPost); //获取结果实体 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity, "UTF-8"); } EntityUtils.consume(entity); //释放连接 response.close(); System.out.println("body:" + body); }finally{ client.close(); } }
https地址以豆瓣的一个api为例,得到ID为1220562的书的信息。
运行代码:
获取返回信息成功。
本博客例子下载地址:
http://download.csdn.net/download/irokay/10158259
例子中包含以上工程代码,以及所需HttpClient组件jar库。
参考:
http://www.javashuo.com/article/p-vtyjicqx-dm.html
http://blog.csdn.net/xiaoxian8023/article/details/49865335
http://www.javashuo.com/article/p-vtyjicqx-dm.html