HttpClient请求及代理IP请求


    org.apache.httpcomponents
    httpclient
    4.5.4


    
    org.jsoup
    jsoup
    1.10.3

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 parameters = new ArrayList();
         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);
}

你可能感兴趣的:(HttpClient请求及代理IP请求)