public static boolean post() throws Exception{
//1 得到浏览器
HttpClient httpClient = new DefaultHttpClient();
//2 指定请求方式
HttpPost httpPost = new HttpPost(posturl);
httpPost.addHeader("Cookie", "");
httpPost.addHeader("User-Agent", "");
//3构建请求实体的数据
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("password", password));
//4 构建实体
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
//5 把实体数据设置到请求对象
httpPost.setEntity(entity);
//6 执行请求
HttpResponse httpResponse = httpClient.execute(httpPost);
//7 判断请求是否成功
if(httpResponse.getStatusLine().getStatusCode() == 200){
return true;
}
return false;
}
模拟POST请求
最新推荐文章于 2022-08-08 10:51:35 发布