1. 程式人生 > >接口測試——HttpClient工具的https請求、代理設置、請求頭設置、獲取狀態碼和響應頭

接口測試——HttpClient工具的https請求、代理設置、請求頭設置、獲取狀態碼和響應頭

能夠 zh-cn www 接口 連接方式 協議 custom headers 兩個

轉自: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請求、代理設置、請求頭設置、獲取狀態碼和響應頭