目錄
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通路:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL1ADM1YDN3czMx0iN1MzMwMTNwITMwATM3EDMy0yNyIjM0ITMvwFMxcTMwIzLcdjMyIDNyEzLcd2bsJ2Lc12bj5ycn9Gbi52YucTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
浏覽器通路:
可以很清楚的看出,各自的請求頭不同
設定請求頭的方法有三種實作方法:
第一種實作代碼如下
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 }
周遊輸出所有響應頭内容,如下所示: