<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);
}