HttpClient4.x之Get請求示例
阿新 • • 發佈:2018-11-15
在使用的HttpClient的之前先了解一下它是什麼,為了儘可能的展示其最為原味的介紹,我們就去他的官網看看。http://hc.apache.org/httpcomponents-client-ga/ 首頁繁體繁體內容如下:
該圖片內容是通過谷歌遊覽器自帶翻譯外掛進行翻譯的內容。
我在實際工作中一般進行後臺跨域和不同服務介面呼叫使用比較頻繁,自己拿他做過一些簡單的爬蟲。後面會給大家寫一篇實現百度搜索的爬蟲。
開始我們的部落格主題內容:。獲取請求示例,在開始介紹之前我們需要進行一寫準備工作確保已經安裝JDK和Maven的環境,並對JDK和行家有一定的瞭解另外開發工具這裡我這裡使用的是 :Spring Tool Suite(STS)(當然你也可以使用其他的開發工具進行。環境和版本說明大致如下:
開發工具:Spring Tool Suite(STS) 3.9.6.RELEASE
maven版本:3.2.5
jdk版本: 1.8.0_144
首先新增的的HttpClient的依賴
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency>
引入的log4j的的依賴
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
log4j.properties
log4j.rootLogger=INFO,stdout
### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
獲取請求示例操作流程
1我們要先建立HttpClient的物件
2建立HTTPGET物件並通過構造設定訪問的網址以及呼叫引數
3執行的HttpClient的執行方法發起GET請求並返回CloseableHttpResponse物件
4從CloseableHttpResponse物件中獲取響應狀態以及響應內容。
獲得請求示例演示程式詳細程式碼:
package cn.zhuoqianmingyue.getoperation;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class SimpleGetHttpClientDemo {
private static Log log = LogFactory.getLog(SimpleGetHttpClientDemo.class );
/**
* 無引數的get訪問
*/
@Test
public void withoutParameters() {
//建立HttpClinet
CloseableHttpClient httpClient = HttpClients.createDefault();
//新增HTTP GET請求 訪問百度首頁
HttpGet httpGet = new HttpGet("https://www.baidu.com");
CloseableHttpResponse response = null;
try {
//執行請求訪問
response = httpClient.execute(httpGet);
//獲取返回HTTP狀態碼
int satausCode = response.getStatusLine().getStatusCode();
if(satausCode == 200 ){
String content = EntityUtils.toString(response.getEntity(),"UTF-8");
log.info("百度首頁頁面:"+content);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 有引數的訪問
* @throws URISyntaxException
*/
@Test
public void withParameters() throws URISyntaxException {
//建立HttpClinet
CloseableHttpClient httpClient = HttpClients.createDefault();
//拼接訪問url 進行
URI uri = new URI("http://www.baidu.com/s");
//拼接搜尋內容 ?wd=httpclinet
URIBuilder uriBuilder = new URIBuilder(uri);
uriBuilder.setParameter("wd", "httpclient");
URI uriParma = uriBuilder.build();
//新增HTTP GET請求 訪問百度搜索httpclient相關資訊
HttpGet httpGet = new HttpGet(uriParma);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
int satausCode = response.getStatusLine().getStatusCode();
if(satausCode == 200 ){
String content = EntityUtils.toString(response.getEntity(),"UTF-8");
log.info(content);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 訪問https://www.baidu.com 搜尋需要設定請求頭的 Host:www.baidu.com
* @throws URISyntaxException
*/
@Test
public void withParametersByHttps() throws URISyntaxException {
//建立HttpClinet
CloseableHttpClient httpClient = HttpClients.createDefault();
//拼接訪問url 進行
URI uri = new URI("https://www.baidu.com/s");
//拼接搜尋內容 ?wd=httpclinet
URIBuilder uriBuilder = new URIBuilder(uri);
uriBuilder.setParameter("wd", "httpclient");
URI uriParma = uriBuilder.build();
//新增HTTP GET請求 訪問百度搜索httpclient相關資訊
HttpGet httpGet = new HttpGet(uriParma);
httpGet.addHeader("Host","www.baidu.com");
httpGet.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
int satausCode = response.getStatusLine().getStatusCode();
if(satausCode == 200 ){
String content = EntityUtils.toString(response.getEntity(),"UTF-8");
log.info(content);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}