cnzz 模拟请求登录(传入url get data ) demo
2024-09-27 11:35:31
/**
*
* @Title: init
* @Description: TODO 初始化httpclien
* @param url
* cnzz对应的链接
* @param password
* cnzz 对应的密码
* @return
*
* @return: HttpClient
*/
public static String init(HttpClient httpclient, String url, String password) {
// 建立 httpPost对象
HttpPost httpPost = new HttpPost(url);
// 建立一个NameValuePair数组,用于存储欲传送的参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("password", password));
String locationUrl = "";
try {
// 设置为utf-8编码
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpPost);
// 获取 重定向后的url
locationUrl = response.getLastHeader("Location").getValue();
// 设置cookie
List<Cookie> cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println(cookies.get(i).getName() + ":" + cookies.get(i).getValue() + "-----");
}
}
// 获得返回体
HttpEntity entity = response.getEntity();
// 获得体内容
String responseHtmQueryPage = IOUtils.toString(entity.getContent());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return locationUrl; } /**
*
* @Title: sendRequest
* @Description: TODO 根据登陆后跳转的链接发送 第二次请求
* @param httpclient
* @param locationUrl
* 登陆后重定向的链接
*
* @return: url
*/
public static String sendTwoRequest(HttpClient httpclient, String locationUrl) {
String url = "";
try {
// 发送get请求
HttpGet httpGet = new HttpGet(locationUrl);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
String responseHtmQueryPage = IOUtils.toString(entity.getContent());
Document document = Jsoup.parse(responseHtmQueryPage);
url = "https://web.umeng.com/" + document.select("script").html().split("'")[1];
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return url;
} /**
*
* @Title: sendRequest
* @Description: TODO 发送第三次请求
* @param httpclient
* @param url
* @return
*
*
*/
public static void sendThreeRequest(HttpClient httpclient, String url) { try {
// 发送get请求
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
String responseHtmQueryPage = IOUtils.toString(entity.getContent()); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /**
*
* @Title: sendFourRequest
* @Description: TODO 发送第四次请求 及要获取的真正数据的请求
* @param httpclient
* @param realUrl
* @return
*
* @return: String
*/
public static String sendFourRequest(HttpClient httpclient, String realUrl) {
// 发送get请求
String responseHtmQueryPage = "";
try {
HttpGet httpGet = new HttpGet(realUrl); HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
responseHtmQueryPage = IOUtils.toString(entity.getContent());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return responseHtmQueryPage; }
最新文章
- 谈谈RPC中的异步调用设计
- TID大会学习心得之软技能
- Android DiskLruCache 源码解析 硬盘缓存的绝佳方案
- html表单验证程序
- js面形对象(2)
- ToString()字符转换类型
- WPF技术触屏上的应用系列(一): 3D 图片(照片)墙、柱面墙(凹面墙或者叫远景墙、凸面墙或者叫近景墙)实现
- Java模拟post提交表单数据
- 2017 UESTC Training for Data Structures
- 转: web 页面加载速度优化实战-100% 的飞跃提升
- [Swift]LeetCode897. 递增顺序查找树 | Increasing Order Search Tree
- Flask 系列之 优化项目结构
- eclipse添加mybatis插件
- Object类--equals方法
- async 常用函数总结
- SSM搭建Spring单元测试环境
- WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
- [原]C# 常用函数统计
- BPM与ESB
- cxf soap rest webservice spring