java cnzz_cnzz 模拟请求登录(传入url get data ) demo

/**

*

* @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 params = new ArrayList();

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 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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值