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);
// 创建代理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);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,280评论 19 139
  • 上一篇《WEB请求处理一:浏览器请求发起处理》,我们讲述了浏览器端请求发起过程,通过DNS域名解析服务器IP,并建...
    七寸知架构阅读 81,397评论 21 356
  • AFHTTPRequestOperationManager 网络传输协议UDP、TCP、Http、Socket、X...
    Carden阅读 5,104评论 0 12
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,311评论 6 13
  • 亲爱的S: 这软件真好!发现很适合我这个平时热爱絮叨但是又不喜欢语音的人。 我都忘了 我有没有发这张照...
    望不到边的蓝阅读 301评论 0 0