下面分別介紹使用HTTPClient和CloseableHTTPClient進行Get和Post請求的方式。java
使用commons-httpclient.jar,maven依賴以下:apache
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
複製代碼
java示例代碼以下:maven
package com.example.study.demo.http;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.IOException;
/** * HttpClient使用示例 * jar包:commons-httpclient.jar */
public class HttpClientTest {
private static void doGet() {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("https://www.thepaper.cn/");
int code = 0;
try {
code = client.executeMethod(getMethod);
if (code == 200) {
String res = getMethod.getResponseBodyAsString();
System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void doPost() {
String praiseUrl = "https://www.thepaper.cn/www/commentPraise.msp"; // 澎湃新聞評論點贊url
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(praiseUrl);
// 必須設置下面這個Header
postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
postMethod.addParameter("commentId", "18718372"); // 評論的id,抓包得到
try {
int code = client.executeMethod(postMethod);
if (code == 200) {
String res = postMethod.getResponseBodyAsString();
System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
doGet();
System.out.println("-----------分割線------------");
System.out.println("-----------分割線------------");
System.out.println("-----------分割線------------");
doPost();
}
}
複製代碼
使用httpclient.jar,maven依賴以下:post
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
複製代碼
java示例代碼以下:ui
package com.example.study.demo.http;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/** * CloseableHttpClient使用示例 * jar包:httpclient.jar */
public class CloseableHttpClientTest {
public static void doGet() {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet("http://www.thepaper.cn");
try {
// 很奇怪,使用CloseableHttpClient來請求澎湃新聞的首頁,GTE請求也必須加上下面這個Header,可是使用HTTPClient則不須要
get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
HttpResponse response = client.execute(get);
String res = EntityUtils.toString(response.getEntity());
System.out.println(res);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void doPost() {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://www.thepaper.cn/www/commentPraise.msp");
try {
post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("commentId", "18718372"));
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = client.execute(post);
String res = EntityUtils.toString(response.getEntity());
System.out.println(res);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
doGet();
System.out.println("-----------分割線------------");
System.out.println("-----------分割線------------");
System.out.println("-----------分割線------------");
doPost();
}
}
複製代碼