使用httpclient4.3.X模拟post请求登陆网站获取cookie信息的操作

             众所周知,httpclient可以模拟登陆操作,下面我就来个实例测试一下,顺便也回忆下这个开源工具的使用,直接上例子吧!

  


package com.pyc.search.crawler.node.login;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class LoginMis {
	private CloseableHttpClient httpclient;
	private HttpPost httppost;// 用于提交登陆数据
	private HttpGet httpget;// 用于获得登录后的页面
	private String login_success;// 用于构造上面的HttpGet

	public LoginMis() {
		httpclient = HttpClients.createDefault();
		// mis登陆界面网址
		httppost = new HttpPost("http://mis.pyc.com.cn/login.aspx");
	}

	public void logIn(String name, String password) throws Exception {
		httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
		String post ="__VIEWSTATE=%2FwEPDwUJNjUwNzE0MTM4ZGRq%2FuXaVF47TNHr5WWnDm%2F6haaRp0gOdDNIQoZp%2FJkyIA%3D%3D&__EVENTVALIDATION=%2FwEWBQLc1Oq3DgLEhISFCwKd%2B7qdDgKC3IeGDAK7q7GGCL0B2uWAzgYaqwKD%2F9eVKMfwK6lpSiVQr9EOO89rWz0w&txtName="+name+"&txtPwd="+password+"&btnLogin=%E7%99%BB+%E5%BD%95";
		httppost.setEntity(new StringEntity(post, "utf-8"));

		try {
			// 提交登录数据
			HttpResponse re = httpclient.execute(httppost);
			
			System.out.println("response:	"+re);
			
			
			Header[] h =re.getAllHeaders();
			for (Header header : h) {
				System.out.println(header.toString());
			}
			// 获得跳转的网址
			Header locationHeader = re.getFirstHeader("Location");
			
			
			// 登陆不成功
			if (locationHeader == null) {
				System.out.println("用户 :   "+name+"登陆不成功,请确认您的用户名和密码正确...");
				System.out.println("用户 :   "+name+" \t密码:"+password);
				return;
			} else// 成功
			{
				login_success = "http://mis.pyc.com.cn"+locationHeader.getValue();// 获取登陆成功之后跳转链接
				System.out.println("用户: "+name+"  登陆成功,之后跳转到的网页网址:" + login_success);
				TimeUnit.SECONDS.sleep(3);
				System.out.println("用户: "+name+"  cookie信息: "+re.getLastHeader("Set-Cookie").getValue());
				TimeUnit.SECONDS.sleep(3);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void PrintText(String name) throws IOException {
		httpget = new HttpGet(login_success);
		HttpResponse re2 = null;

		try {
			re2 = httpclient.execute(httpget);
			// 输出登录成功后的页面
			String str = EntityUtils.toString(re2.getEntity());
			
			System.out.println("\n"+name+"首页信息如下:");
			System.out.println(str.substring(8250,8400));
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			httppost.abort();
			httpget.abort();
			httpclient.close();
		}
	}

	public static void main(String[] args) throws Exception {
		String name = "xxxxx", password = "xxxxxx";
		// 自己的账号,口令
		LoginMis lr = new LoginMis();
		lr.logIn(name, password);
		lr.PrintText(name);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值