①用Fiddler2追踪登录时的post请求,发现需要以下参数:

check

uname

backURL

autoLogin

pwd

其中,backURL="/",check=“1”,autoLogin可默认为1

于是,只剩 uname和 pwd

②创建一个HttpClient

private DefaultHttpClient httpclient = new DefaultHttpClient();

③创建一个 HttpPost

HttpPost httpost = new HttpPost(CommonConst.loginUrl);

④伪装httpost,骗过服务器

    /**
     * pretend to be a browser quietly
     
*/
    
private void setPostHeader(HttpPost post) {
        post.setHeader(CommonConst.UserAgent, CommonConst.HttpAgent);
        post.setHeader(
"Origin", CommonConst.weiboUrl);
        post.setHeader(
"Cache-Control""max-age=0");
        post.setHeader(
"Accept",
                
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        post.setHeader(
"Accept-Encoding""gzip,deflate,sdch");
        post.setHeader(
"Accept-Language""en-US,en;q=0.8");
        post.setHeader(
"Accept-Charset""ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        post.setHeader(
"Accept-Encoding""gzip,deflate,sdch");
        post.setHeader(
"Referer", CommonConst.loginUrl);
    }

 ⑤创建NameValuePair

List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(
new BasicNameValuePair("check"this.check));
        nvps.add(
new BasicNameValuePair("uname"this.uname));
        nvps.add(
new BasicNameValuePair("backURL"this.backURL));
        nvps.add(
new BasicNameValuePair("autoLogin"this.autoLogin));
        nvps.add(
new BasicNameValuePair("pwd"this.pwd));

⑥用setEntity方法,给httpost设置相关参数

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

⑦向相应的host上提交post请求

HttpHost targetHost = new HttpHost(CommonConst.host);
            response 
= httpclient.execute(targetHost, httpost);


login代码:

private boolean login() {
        
// httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
        HttpPost httpost = new HttpPost(CommonConst.loginUrl);
        setPostHeader(httpost);
        
// All the parameters post to the web site
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(
new BasicNameValuePair("check"this.check));
        nvps.add(
new BasicNameValuePair("uname"this.uname));
        nvps.add(
new BasicNameValuePair("backURL"this.backURL));
        nvps.add(
new BasicNameValuePair("autoLogin"this.autoLogin));
        nvps.add(
new BasicNameValuePair("pwd"this.pwd));

        
try {
            httpost.setEntity(
new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            HttpHost targetHost 
= new HttpHost(CommonConst.host);
            response 
= httpclient.execute(targetHost, httpost);
        } 
catch (Exception e) {
            e.printStackTrace();
            
return false;
        } 
finally {
            httpost.abort();
        }
        
return true;
    }


附CommonConst.java

package com.yinger;

public class CommonConst {
    
public static String HttpAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5";
    
public static String loginUrl = "http://m.weibo.cn/login";
    
public static String host = "m.weibo.cn";
    
public static String weiboUrl = "http://m.weibo.cn";
    
public static String UserAgent = "User-Agent";
}



posted on 2012-07-11 11:59 Ying-er 阅读(5217) 评论(5)  编辑  收藏

评论:
# re: 用httpclient模拟浏览器,登录新浪微博 2012-09-11 14:36 |
能否问下,用Fiddler2 如何追踪到这些参数的。请教一下  回复  更多评论
  
# re: 用httpclient模拟浏览器,登录新浪微博[未登录] 2012-11-24 00:17 | tang
what library should we included in it ?  回复  更多评论
  
# re: 用httpclient模拟浏览器,登录新浪微博 2013-01-06 14:51 | auto714
HttpClient 开源包@tang
  回复  更多评论
  
# re: 用httpclient模拟浏览器,登录新浪微博 2013-01-06 14:52 | auto714
亲!有尝试过发送微博么?  回复  更多评论
  
# re: 用httpclient模拟浏览器,登录新浪微博 2013-01-09 11:04 | restart
扯呢么,Fiddler2 得到的参数,远不止这些。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: