网络请求基础
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包。
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可:
-
创建CloseableHttpClient对象。
-
创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
-
如果需要发送请求参数,可可调用setEntity(HttpEntity entity)方法来设置请求参数。setParams方法已过时(4.4.1版本)。
-
调用HttpGet、HttpPost对象的setHeader(String name, String value)方法设置header信息,或者调用setHeaders(Header[] headers)设置一组header信息。
-
调用CloseableHttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个CloseableHttpResponse。
-
调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容;调用CloseableHttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头。
-
释放连接。无论执行方法是否成功,都必须释放连接
Demo:
/** * 简单httpclient实例 * @Author 王志学 * @Data 2017/3/3 */ public class SimpleHttpClientDemo { /** * 模拟请求 * @param url 资源地址 * @param map 参数列表 * @param encoding 编码 * @return */ public static String send(String url, Map<String,String> map, String encoding) throws IOException { String body = ""; //创建httpclient对象 CloseableHttpClient client = HttpClients.createDefault(); //创建post方法请求对象 HttpPost post = new HttpPost(url); //装填参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(map!=null){ for (Map.Entry<String,String> entry : map.entrySet()){ nvps.add(new BasicNameValuePair(entry.getKey(),entry.getValue())); } } //设置参数到请求对象中 post.setEntity(new UrlEncodedFormEntity(nvps,encoding)); System.out.println("请求地址:" + url); System.out.println("请求参数:" + nvps.toString()); //设置header信息 //制定报文【Content-type】、【User-Agent】 post.setHeader("ip","192.168.1.1"); post.setHeader("Content-type", "application/x-www-form-urlencoded"); post.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //执行请求操作,并拿到结果(同步阻塞) CloseableHttpResponse response = client.execute(post); //获取结果体 HttpEntity entity = response.getEntity(); if (entity!=null){ //按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity,encoding); } //确保内容全部加载,否则关闭关闭流 EntityUtils.consume(entity); response.close(); return body; } }
HttpClient设置ssl->绕过证书验证https
1、绕过证书验证
对方的证书没有通过ca认证或者其他问题导致证书不被信任。所以对于这样的情况,你只能是选择绕过证书验证的方案了。
/** * @Author 王志学 * @Data 2017/3/3 */ public class SSLHttpClientDemo { /** * 绕过验证 * @return * @throws NoSuchAlgorithmException */ public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sc = SSLContext.getInstance("SSLv3"); // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法 X509TrustManager trustManager = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; sc.init(null,new TrustManager[]{trustManager},null); return sc; } /** * 模拟请求 * * @param url 资源地址 * @param map 参数列表 * @param encoding 编码 * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws IOException * @throws ClientProtocolException */ public static String send(String url, Map<String,String> map, String encoding) throws KeyManagementException, NoSuchAlgorithmException, ClientProtocolException, IOException { 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(); //创建post方式请求对象 HttpPost httpPost = new HttpPost(url); //装填参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(map!=null){ for (Map.Entry<String, String> entry : map.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //设置参数到请求对象中 httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); System.out.println("请求地址:"+url); System.out.println("请求参数:"+nvps.toString()); //设置header信息 //指定报文头【Content-type】、【User-Agent】 httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //执行请求操作,并拿到结果(同步阻塞) CloseableHttpResponse response = client.execute(httpPost); //获取结果实体 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity, encoding); } EntityUtils.consume(entity); //释放链接 response.close(); return body; } }
2、自己信任证书 如果是自己用jdk或者其他工具生成的证书,还是希望用其他方式认证自签名的证书