接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头
目录
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访问博客园中的新闻页面,实现代码如下:
1 package com.httpclient; 2 3 import java.io.IOException; 4 import java.security.KeyManagementException; 5 import java.security.KeyStoreException; 6 import java.security.NoSuchAlgorithmException; 7 import java.security.cert.CertificateException; 8 import java.security.cert.X509Certificate; 9 10 import javax.net.ssl.SSLContext; 11 12 import org.apache.http.HttpEntity; 13 import org.apache.http.client.ClientProtocolException; 14 import org.apache.http.client.methods.CloseableHttpResponse; 15 import org.apache.http.client.methods.HttpGet; 16 import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 17 import org.apache.http.conn.ssl.SSLContextBuilder; 18 import org.apache.http.conn.ssl.TrustStrategy; 19 import org.apache.http.impl.client.CloseableHttpClient; 20 import org.apache.http.impl.client.HttpClients; 21 import org.apache.http.util.EntityUtils; 22 23 public class yihuqingjiu_https { 24 25 public static CloseableHttpClient createSSLClientDefault(){ 26 try { 27 SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){ 28 //信任所有 29 public boolean isTrusted(X509Certificate[] chain,String authType) throws CertificateException{ 30 return true; 31 } 32 }).build(); 33 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); 34 return HttpClients.custom().setSSLSocketFactory(sslsf).build(); 35 } catch (KeyManagementException e) { 36 e.printStackTrace(); 37 } catch (NoSuchAlgorithmException e) { 38 e.printStackTrace(); 39 } catch (KeyStoreException e) { 40 e.printStackTrace(); 41 } 42 return HttpClients.createDefault(); 43 } 44 45 public static void main(String[] args) throws ClientProtocolException, IOException { 46 CloseableHttpClient hp = createSSLClientDefault(); 47 HttpGet hg = new HttpGet("https://news.cnblogs.com/"); 48 CloseableHttpResponse response = hp.execute(hg); 49 HttpEntity entity = response.getEntity(); 50 String content = EntityUtils.toString(entity,"utf-8"); 51 System.out.println(content); 52 hp.close(); 53 54 } 55 56 }
若不添加信任,createSSLClientDefault()方法,会访问失败
代理设置
代理,也称网络代理,是一种特殊的网络服务, 允许一个网络终端(一般为客户端) 通过这个服务与另一个网络终端(一般为服务器) 进行非直接的连接。 一些网关、 路由器等网络设备具备网络代理功能。 一般认为代理服务有利于保障网络终端的隐私或安全, 防止攻击。在使用httpclient进行接口测试的时候, 出现需要访问国外的接口请求地址、使用fiddler调试等时候需要先设置代理才能进行。
fiddler会自动给浏览器加上127.0.0.1:8888,但java代码中fiddler不会自动给加上。运行上述实例,但在fiddler中抓取不到,这就需要进行代理设置了。
代码实现如下:
1 package com.httpclient; 2 3 import java.io.IOException; 4 5 import org.apache.http.HttpEntity; 6 import org.apache.http.HttpHost; 7 import org.apache.http.client.ClientProtocolException; 8 import org.apache.http.client.config.RequestConfig; 9 import org.apache.http.client.methods.CloseableHttpResponse; 10 import org.apache.http.client.methods.HttpGet; 11 import org.apache.http.impl.client.CloseableHttpClient; 12 import org.apache.http.impl.client.HttpClients; 13 import org.apache.http.util.EntityUtils; 14 15 public class yihuqingjiu_Proxy { 16 17 public static void main(String[] args) throws ClientProtocolException, IOException { 18 //创建httpclient对象 19 CloseableHttpClient httpClient = HttpClients.createDefault(); 20 //代理对象 21 HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http"); 22 //配置对象 23 RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); 24 //创建请求方法的实例, 并指定请求url 25 HttpGet httpget=new HttpGet("http://www.qq.com"); 26 //使用配置 27 httpget.setConfig(config); 28 CloseableHttpResponse response=httpClient.execute(httpget); 29 HttpEntity entity=response.getEntity(); 30 String content=EntityUtils.toString(entity, "utf-8"); 31 System.out.println(content); 32 httpClient.close(); 33 34 } 35 36 }
请求头设置
有很多服务器,会辨别访问形式是否为浏览器,若不是浏览器,会拒绝访问,所以就需要设置请求头
当我们打开一个网页时, 浏览器要向网站服务器发送一个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中查看请求头信息,和浏览器访问一样了,如下图所示:
第二中实现方式,创建代理对象,代码如下:
1 package com.httpclient; 2 3 import java.io.IOException; 4 5 import org.apache.http.HttpEntity; 6 import org.apache.http.HttpHost; 7 import org.apache.http.client.ClientProtocolException; 8 import org.apache.http.client.config.RequestConfig; 9 import org.apache.http.client.methods.CloseableHttpResponse; 10 import org.apache.http.client.methods.HttpGet; 11 import org.apache.http.entity.mime.Header; 12 import org.apache.http.impl.client.CloseableHttpClient; 13 import org.apache.http.impl.client.HttpClients; 14 import org.apache.http.message.BasicHeader; 15 import org.apache.http.util.EntityUtils; 16 17 import com.google.common.net.HttpHeaders; 18 19 public class yihuqingjiu_header1 { 20 21 public static void main(String[] args) throws ClientProtocolException, IOException { 22 //创建httpclient对象 23 CloseableHttpClient httpClient = HttpClients.createDefault(); 24 //代理对象 25 HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http"); 26 //配置对象 27 RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); 28 //创建请求方法的实例, 并指定请求url 29 HttpGet httpget=new HttpGet("http://www.qq.com"); 30 //使用配置 31 httpget.setConfig(config); 32 //设置请求头,对象实现 33 BasicHeader a = new BasicHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); 34 httpget.setHeader(a); 35 CloseableHttpResponse response=httpClient.execute(httpget); 36 HttpEntity entity=response.getEntity(); 37 String content=EntityUtils.toString(entity, "utf-8"); 38 System.out.println(content); 39 httpClient.close(); 40 41 } 42 43 }
第三种实现方式,数组实现,代码如下:
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中的第一行数据,获取状态码实现代码如下:
1 package com.httpclient; 2 3 import java.io.IOException; 4 5 import org.apache.http.HttpEntity; 6 import org.apache.http.HttpHost; 7 import org.apache.http.client.ClientProtocolException; 8 import org.apache.http.client.config.RequestConfig; 9 import org.apache.http.client.methods.CloseableHttpResponse; 10 import org.apache.http.client.methods.HttpGet; 11 import org.apache.http.impl.client.CloseableHttpClient; 12 import org.apache.http.impl.client.HttpClients; 13 import org.apache.http.util.EntityUtils; 14 15 public class yihuqingjiu_response1 { 16 17 public static void main(String[] args) throws ClientProtocolException, IOException { 18 //创建httpclient对象 19 CloseableHttpClient httpClient = HttpClients.createDefault(); 20 //代理对象 21 HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http"); 22 //配置对象 23 RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); 24 //创建请求方法的实例, 并指定请求url 25 HttpGet httpget=new HttpGet("http://www.qq.com"); 26 //使用配置 27 httpget.setConfig(config); 28 CloseableHttpResponse response=httpClient.execute(httpget); 29 HttpEntity entity=response.getEntity(); 30 String content=EntityUtils.toString(entity, "utf-8"); 31 System.out.println(content); 32 System.out.println("------------------------------------"); 33 //获取响应状态码 34 int code = response.getStatusLine().getStatusCode(); 35 System.out.println("code:"+code); 36 String a = response.getStatusLine().toString(); 37 System.out.println("a:"+a); 38 httpClient.close(); 39 } 40 41 }
接收响应头
响应头也是Headers中的内容,如下图所示:
实现代码如下所示,里面包含多种实现方式,但输出的内容都差不多
1 package com.httpclient; 2 3 import java.io.IOException; 4 5 import org.apache.http.Header; 6 import org.apache.http.HttpEntity; 7 import org.apache.http.HttpHost; 8 import org.apache.http.client.ClientProtocolException; 9 import org.apache.http.client.config.RequestConfig; 10 import org.apache.http.client.methods.CloseableHttpResponse; 11 import org.apache.http.client.methods.HttpGet; 12 import org.apache.http.impl.client.CloseableHttpClient; 13 import org.apache.http.impl.client.HttpClients; 14 import org.apache.http.util.EntityUtils; 15 16 public class yihuqingjiu_response { 17 18 public static void main(String[] args) throws ClientProtocolException, IOException { 19 //创建httpclient对象 20 CloseableHttpClient httpClient = HttpClients.createDefault(); 21 //代理对象 22 HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http"); 23 //配置对象 24 RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); 25 //创建请求方法的实例, 并指定请求url 26 HttpGet httpget=new HttpGet("http://www.qq.com"); 27 //使用配置 28 httpget.setConfig(config); 29 CloseableHttpResponse response=httpClient.execute(httpget); 30 HttpEntity entity=response.getEntity(); 31 String content=EntityUtils.toString(entity, "utf-8"); 32 System.out.println(content); 33 System.out.println("------------------------------------"); 34 //接收响应头 35 //获取一个响应头,first和last两个方法指的是,当里面有两个一样的响应时,就去第一个或最后一个 36 String server = response.getFirstHeader("Server").toString(); 37 System.out.println(server); 38 //获取所有响应头 39 Header[] header = response.getAllHeaders(); 40 //遍历输出所有 41 for(Header as:header){ 42 System.out.println(as.toString()); 43 } 44 System.out.println("------------------------------------"); 45 //输出name 46 for(Header name:header){ 47 System.out.println(name.getName()); 48 } 49 System.out.println("------------------------------------"); 50 //输出value 51 for(Header value:header){ 52 System.out.println(value.getValue()); 53 } 54 //输出第一个 55 //System.out.println(header[0].toString()); 56 //输出数组大小 57 Header[] ha = response.getHeaders("Server"); 58 System.out.println(ha.length); 59 httpClient.close(); 60 61 } 62 63 }
遍历输出所有响应头内容,如下所示: