总体步骤:
-
创建HttpClient对象
-
输入网址
-
发起请求
-
解析响应
-
POST请求
上代码
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 爬虫post请求
*/
public class SpyderPostTest {
public static void main(String[] args) throws Exception {
// 创建HttpClient对象
HttpClient httpClient = HttpClients.createDefault();
// 设置参数
HttpPost httpPost = new HttpPost("https://www.baidu.com/serach");
// 创建参数
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("keys", "java"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf8");
httpPost.setEntity(formEntity);
// 发起请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
// 解析响应
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, "utf8");
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}