67,513
社区成员
发帖
与我相关
我的任务
分享
package com.java.activiti.controller;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class python {
public static void main(String[] args) {
String url = "http://liuyan.people.com.cn/threads/queryThreadsList?fid=1079&lastItem=0";
HttpClient httpClient = null;
HttpPost httpPost = null;
HttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);// 传入URL地址
httpPost.addHeader("Accept",
"pplication/json, text/javascript, */*; q=0.01");
httpPost.addHeader("Accept-Encoding", "gzip, deflate");// 设置请求头
httpPost.addHeader("Accept-Language",
"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");// 设置请求头
//httpPost.addHeader("Connection", "keep-alive");// 设置请求头
// httpPost.addHeader("Content-Length", "19");//设置请求头
//httpPost.addHeader("Content-Type",
// "application/x-www-form-urlencoded; charset=UTF-8");// 设置请求头
httpPost.addHeader("Referer",
"http://liuyan.people.com.cn/threads/list?fid=1079");// 设置请求头
//httpPost.addHeader("User-Agent",
// "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/64.0");// 设置请求头
//httpPost.addHeader("X-Requested-With", "XMLHttpRequest");// 设置请求头
response = httpClient.execute(httpPost);// 获取响应
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("HTTP Status Code:" + statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.out.println("HTTP请求未成功!HTTP Status Code:"
+ response.getStatusLine());
}
HttpEntity httpEntity = response.getEntity();
String reponseContent = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
System.out.println("响应内容:" + reponseContent);
} catch (Exception e) {
e.printStackTrace();
}
}
}