码迷,mamicode.com
首页 > Web开发 > 详细

接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头

时间:2018-09-18 00:30:12      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:响应头   代理   默认   版本   utf-8   生成   custom   info   int   

转自:https://www.cnblogs.com/hong-fithing/p/7617855.html

https请求

https协议(Secure Hypertext Transfer Protocol) :

安全超文本传输协议, HTTPS以保密为目标研发, 简单讲HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、 身份认证的网络协议, 其安全基础是SSL协议, 因此加密的详细内容请看SSL。 全称Hypertext Transfer Protocol overSecure Socket Layer。句法类同http:体系。 用于安全的HTTP数据传输。 https:URL表明它使用了HTTP, 但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。

HTTPS和HTTP的区别:

一、 https协议需要到ca申请证书, 一般免费证书很少, 需要交费。

二、 http是超文本传输协议, 信息是明文传输, https 则是具有安全性的ssl加密传输协议。

三、 http和https使用的是完全不同的连接方式, 用的端口也不一样, 前者是80,后者是443。

四、 http的连接很简单, 是无状态的; HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、 身份认证的网络协议, 比http协议安全。

https访问博客园中的新闻页面,实现代码如下:

package com.httpclient;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class yihuqingjiu_https {

    public static CloseableHttpClient createSSLClientDefault(){
        try {
                SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){
                //信任所有
                public boolean isTrusted(X509Certificate[] chain,String authType) throws CertificateException{
                return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyStoreException e) {
                e.printStackTrace();
            } 
            return HttpClients.createDefault();
            }
    
    public static void main(String[] args) throws ClientProtocolException, IOException {
        CloseableHttpClient hp = createSSLClientDefault();
        HttpGet hg = new HttpGet("https://news.cnblogs.com/");
        CloseableHttpResponse response = hp.execute(hg);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity,"utf-8");
        System.out.println(content);
        hp.close();

    }

}

若不添加信任,createSSLClientDefault()方法,会访问失败

代理设置

代理,也称网络代理,是一种特殊的网络服务, 允许一个网络终端(一般为客户端) 通过这个服务与另一个网络终端(一般为服务器) 进行非直接的连接。 一些网关、 路由器等网络设备具备网络代理功能。 一般认为代理服务有利于保障网络终端的隐私或安全, 防止攻击。在使用httpclient进行接口测试的时候, 出现需要访问国外的接口请求地址、使用fiddler调试等时候需要先设置代理才能进行。

fiddler会自动给浏览器加上127.0.0.1:8888,但java代码中fiddler不会自动给加上。运行上述实例,但在fiddler中抓取不到,这就需要进行代理设置了。

代码实现如下:

package com.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

public class yihuqingjiu_Proxy {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //代理对象
        HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
        //配置对象
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        //创建请求方法的实例, 并指定请求url
        HttpGet httpget=new HttpGet("http://www.qq.com");
        //使用配置
        httpget.setConfig(config);
        CloseableHttpResponse response=httpClient.execute(httpget);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        System.out.println(content);
        httpClient.close();

    }

}

请求头设置

有很多服务器,会辨别访问形式是否为浏览器,若不是浏览器,会拒绝访问,所以就需要设置请求头

当我们打开一个网页时, 浏览器要向网站服务器发送一个HTTP请求头, 然后网站服务器根据HTTP请求头的内容生成当次请求的内容发送给浏览器。HTTP请求头提供了关于请求, 响应或者其他的发送实体的信息。 HTTP的头信息包括通用头、 请求头、 响应头和实体头四个部分。 每个头域由一个域名, 冒号(:) 和域值三部分组成。

部分请求头属性介绍:

accept:浏览器通过这个头告诉服务器, 它所支持的数据类型。 如: text/html,image/jpeg

accept-Charset:浏览器通过这个头告诉服务器, 它支持哪种字符集

accept-encoding:浏览器通过这个头告诉服务器, 它支持哪种压缩格式

accept-language:浏览器通过这个头告诉服务器, 它的语言环境

host:浏览器通过这个头告诉服务器, 它想访问哪台主机

Connection:表示客户端与服务连接类型

User-Agent(用户代理),简称 UA, 它是一个特殊字符串头, 使得服务器能够识别客户端使用的操作系统及版本、 CPU 类型、 浏览器及版本、 浏览器渲染引擎、浏览器语言、 浏览器插件等

首先看httpclient发送的请求和浏览器访问的不同之处

httpclient访问:

技术分享图片

浏览器访问:

技术分享图片

可以很清楚的看出,各自的请求头不同

设置请求头的方法有三种实现方法:

第一种实现代码如下

package com.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

public class yihuqingjiu_header {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //代理对象
        HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
        //配置对象
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        //创建请求方法的实例, 并指定请求url
        HttpGet httpget=new HttpGet("http://www.qq.com");
        //使用配置
        httpget.setConfig(config);
        //设置请求头
        httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        httpget.setHeader("Accept-Encoding", "gzip, deflate");
        httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
        httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36");
        CloseableHttpResponse response=httpClient.execute(httpget);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        System.out.println(content);
        httpClient.close();

    }

}

然后到fiddler中查看请求头信息,和浏览器访问一样了,如下图所示:

技术分享图片

第二中实现方式,创建代理对象,代码如下:

package com.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.mime.Header;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import com.google.common.net.HttpHeaders;

public class yihuqingjiu_header1 {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //代理对象
        HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
        //配置对象
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        //创建请求方法的实例, 并指定请求url
        HttpGet httpget=new HttpGet("http://www.qq.com");
        //使用配置
        httpget.setConfig(config);
        //设置请求头,对象实现
        BasicHeader a = new BasicHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        httpget.setHeader(a);
        CloseableHttpResponse response=httpClient.execute(httpget);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        System.out.println(content);
        httpClient.close();

    }

}

第三种实现方式,数组实现,代码如下:

package com.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.mime.Header;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import com.google.common.net.HttpHeaders;

public class yihuqingjiu_header2 {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //代理对象
        HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
        //配置对象
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        //创建请求方法的实例, 并指定请求url
        HttpGet httpget=new HttpGet("http://www.qq.com");
        //使用配置
        httpget.setConfig(config);
        //设置请求头,数组实现
        BasicHeader[] header = new BasicHeader[2];
        //写法1
        //BasicHeader a = new BasicHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        //header[0]=a;
        //写法2
        header[0] = new BasicHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        header[1] = new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.8");
        httpget.setHeaders(header);
        CloseableHttpResponse response=httpClient.execute(httpget);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        System.out.println(content);
        httpClient.close();

    }

}

获取状态码

可以获取Headers中的信息,也就是Headers中的第一行数据,获取状态码实现代码如下:

package com.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

public class yihuqingjiu_response1 {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //代理对象
        HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
        //配置对象
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        //创建请求方法的实例, 并指定请求url
        HttpGet httpget=new HttpGet("http://www.qq.com");
        //使用配置
        httpget.setConfig(config);
        CloseableHttpResponse response=httpClient.execute(httpget);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        System.out.println(content);
        System.out.println("------------------------------------");
        //获取响应状态码
        int code = response.getStatusLine().getStatusCode();
        System.out.println("code:"+code);
        String a = response.getStatusLine().toString();
        System.out.println("a:"+a);    
        httpClient.close();
    }

}

接收响应头

响应头也是Headers中的内容,如下图所示:

技术分享图片

实现代码如下所示,里面包含多种实现方式,但输出的内容都差不多

package com.httpclient;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

public class yihuqingjiu_response {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //代理对象
        HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
        //配置对象
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        //创建请求方法的实例, 并指定请求url
        HttpGet httpget=new HttpGet("http://www.qq.com");
        //使用配置
        httpget.setConfig(config);
        CloseableHttpResponse response=httpClient.execute(httpget);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        System.out.println(content);
        System.out.println("------------------------------------");
        //接收响应头
        //获取一个响应头,first和last两个方法指的是,当里面有两个一样的响应时,就去第一个或最后一个
        String server = response.getFirstHeader("Server").toString();
        System.out.println(server);
        //获取所有响应头
        Header[] header = response.getAllHeaders();
        //遍历输出所有
        for(Header as:header){
            System.out.println(as.toString());
        }
        System.out.println("------------------------------------");
        //输出name
        for(Header name:header){
            System.out.println(name.getName());
        }
        System.out.println("------------------------------------");
        //输出value
        for(Header value:header){
            System.out.println(value.getValue());
        }
        //输出第一个
        //System.out.println(header[0].toString());
        //输出数组大小
        Header[] ha = response.getHeaders("Server");
        System.out.println(ha.length);
        httpClient.close();

    }

}

遍历输出所有响应头内容,如下所示:

技术分享图片

 

接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头

标签:响应头   代理   默认   版本   utf-8   生成   custom   info   int   

原文地址:https://www.cnblogs.com/yadongliang/p/9665521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!