HttpClient 模拟登陆,保持会话并进行后续操作

package cc.unmi.httpclient; 
 
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.Test;
 
public class HttpClientLogin {
 
    public static void main(String[] args){
        //登陆 Url
        String loginUrl = "http://localhost/unmi/login.html";
 
        //需登陆后访问的 Url
        String dataUrl = "http://localhost/unmi/user_info.html?userid=123456";
 
        HttpClient httpClient = new HttpClient();
 
        //模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
        PostMethod postMethod = new PostMethod(loginUrl);
 
        //设置登陆时要求的信息,一般就用户名和密码,验证码自己处理了
        NameValuePair[] data = {
                new NameValuePair("username", "Unmi"),
                new NameValuePair("password", "123456"),
                new NameValuePair("code", "anyany")
        };
        postMethod.setRequestBody(data);
 
        try {
            //设置 HttpClient 接收 Cookie,用与浏览器一样的策略
            httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
            httpClient.executeMethod(postMethod);
 
            //获得登陆后的 Cookie
            Cookie[] cookies=httpClient.getState().getCookies();
            String tmpcookies= "";
            for(Cookie c:cookies){
                tmpcookies += c.toString()+";";
            }
 
            //进行登陆后的操作
            GetMethod getMethod = new GetMethod(dataUrl);
 
            //每次访问需授权的网址时需带上前面的 cookie 作为通行证
            getMethod.setRequestHeader("cookie",tmpcookies);
 
            //你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
            //例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
            postMethod.setRequestHeader("Referer", "http://unmi.cc");
            postMethod.setRequestHeader("User-Agent","Unmi Spot");
 
            httpClient.executeMethod(getMethod);
 
            //打印出返回数据,检验一下是否成功
            String text = getMethod.getResponseBodyAsString();
            System.out.println(text);
 
        } catch (Exception e) {
            e.printStackTrace();
        }   
    }
}
Basic 验证的简单代码导引,还未亲试:
HttpClient client = new HttpClient();
 
// 1
client.getState().setCredentials(
    new AuthScope("unmi.cc", 80, AuthScope.ANY_REALM),
    new UsernamePasswordCredentials("username", "password")
);
 
// 2
client.getParams().setAuthenticationPreemptive(true);
 
// 3
GetMethod getMothod = new GetMethod("http://unmi.cc/twitter");
 
// 4
getMothod.setDoAuthentication( true );
 
// 5
int status = client.executeMethod( getMothod );



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值