HttpClient請求及代理IP請求

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.4</version>
</dependency>
<dependency>
    <!-- jsoup HTML parser library @ https://jsoup.org/ -->
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.10.3</version>
</dependency>

1、GET請求方式

String url = "https://music.163.com/playlist?id=2456763210";
HttpGet httpGet = new HttpGet(url);
//必要時添加請求頭
httpGet.setHeader("Host", "music.163.com");
httpGet.setHeader("Upgrade-Insecure-Requests", "1");
httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36");
httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
httpGet.setHeader("Referer", "https://music.163.com/");
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
    String html = EntityUtils.toString(response.getEntity(),Charset.forName("utf-8"));
        //Document doc = Jsoup.parse(html);
    System.out.println(html);
}

2、Post請求方式

 String url = "https://music.163.com/weapi/song/lyric?csrf_token=";
         HttpPost httpPost = new HttpPost(url);
         List<NameValuePair> parameters = new ArrayList<NameValuePair>();
         parameters.add(new BasicNameValuePair("Content-Type","application/x-www-form-urlencoded"));
         parameters.add(new BasicNameValuePair("Accept","*/*"));
         parameters.add(new BasicNameValuePair("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36"));
         httpPost.setEntity(new UrlEncodedFormEntity(parameters));
         CloseableHttpClient client = HttpClients.createDefault();
         CloseableHttpResponse response = client.execute(httpPost);
         if(response.getStatusLine().getStatusCode() == 200){
         String html= EntityUtils.toString(response.getEntity());
         System.out.println(html);
         }

3、代理IP方式

//HttpGet httpGet = new HttpGet(url);
HttpPost httpPost = new HttpPost(url);
// 創(chuàng)建代理httpClient
CloseableHttpClient httpClient = HttpClients.custom()
                              .setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost(ip, port))).build();
CloseableHttpResponse response = httpClient.execute(httpPost);
if (200 == response .getStatusLine().getStatusCode()) {
        String html = EntityUtils.toString(response .getEntity(), Charset.forName("utf-8"));
        System.out.println(html);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • 上一篇《WEB請求處理一:瀏覽器請求發(fā)起處理》,我們講述了瀏覽器端請求發(fā)起過程,通過DNS域名解析服務(wù)器IP,并建...
    七寸知架構(gòu)閱讀 81,760評論 21 356
  • AFHTTPRequestOperationManager 網(wǎng)絡(luò)傳輸協(xié)議UDP、TCP、Http、Socket、X...
    Carden閱讀 5,321評論 0 12
  • 國家電網(wǎng)公司企業(yè)標準(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,436評論 6 13
  • 親愛的S: 這軟件真好!發(fā)現(xiàn)很適合我這個平時熱愛絮叨但是又不喜歡語音的人。 我都忘了 我有沒有發(fā)這張照...
    望不到邊的藍閱讀 325評論 0 0

友情鏈接更多精彩內(nèi)容