HttpClient 模拟登录网易微博

      实现核心:代码执行流程,根据抓包工具,模拟浏览器请求步骤走

      private static void testLogin()
      {
            try
            {
                  // api.163-->>api.163.login-->>youdao.reg-->>youdao.crossdomain-->>api.163
                  
                  String callback_url = "http://www.weibss.com";
                  
                  TBlog tblog = new TBlog(AConstants.TBLOG_APP_KEY,AConstants.TBLOG_APP_KEY_SECRET);
                  
                  //callback_url 可加可不加,不加则默认跳转到应用设置上的callback_url
                  RequestToken requestToken = tblog.getOAuthRequestToken();
//                RequestToken requestToken = tblog.getOAuthRequestToken(callback_url);
                  
//                String authenticationURL = requestToken.getAuthenticationURL();
                  HttpClient httpClient = new HttpClient();
                  
//                GetMethod get = new GetMethod(authenticationURL);
//                int status = httpClient.executeMethod(get);
//                System.out.println(status+" "+get.getResponseBodyAsString());
//                
//                printHeaders(get);
//                printCookies(httpClient);
                  
                  
                  String regUrl = "https://reg.163.com/logins.jsp";
                  PostMethod post = new PostMethod(regUrl);
                  
//                (Request-Line)    POST /logins.jsp HTTP/1.1
//                Accept      image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
//                Accept-Encoding   gzip, deflate
//                Accept-Language   zh-cn
//                Cache-Control     no-cache
//                Connection  Keep-Alive
//                Content-Length    228
//                Content-Type      application/x-www-form-urlencoded
//                Cookie      _ntes_nnid=d479e519cb36b62de53ba1779bd23896,0; _ntes_nuid=d479e519cb36b62de53ba1779bd23896; P_INFO=nathan_ni@yahoo.cn|1328081405|0|t|00&99|shh&1328079885&t#shh&null#10#0|&0; USERTRACK=58.246.182.22.1328064735108561
//                Host  reg.163.com
//                Referer      http://api.t.163.com/oauth/authenticate?oauth_token=e41312d4338eb66ade9d5b34da0a3531
//                User-Agent  Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.2; InfoPath.2; .NET CLR 2.0.50727)
                  
                  
//                post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//                post.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
//                post.setRequestHeader("Accept-Charset", "utf-8");
//                post.setRequestHeader("Referer", authenticationURL);
                  
                  
//                post.setRequestHeader("Accept","image/gif, image/jpeg, imasge/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
//                post.setRequestHeader("Accept-Encoding", "gzip, deflate");
//                post.setRequestHeader("Accept-Language", "zh-cn");
//                post.setRequestHeader("Cache-Control", "no-cache");
//                post.setRequestHeader("no-Connection", "Keep-Alive");
//                post.setRequestHeader("Host", "reg.163.com");
//                post.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.2; InfoPath.2; .NET CLR 2.0.50727)");
//                post.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//                post.setRequestHeader("Referer", authenticationURL);
                  
//                _end1 
//                _nacc weibo
//                _nfla 10.0
//                _nlag zh-cn
//                _nlmf 1329187321
//                _nref http://wetui.com/app/t163/myWeiboFrom.html
//                _nres 1024x768
//                _nscd 32-bit
//                _nssn fyzbkphc@163.com
//                _nstm 0
//                _ntit      %u7B2C%u4E09%u65B9%u5BA2%u6237%u7AEF_%u7F51%u6613%u5FAE%u535A
//                _nurl      http://api.t.163.com/oauth/authenticate?oauth_token=bcee86830de3bb966fb512603b0077ee
//                _nvfi 0
//                _nvid d479e519cb36b62de53ba1779bd23896
//                _nvsf 0
//                _nvtm 0
//                _nxkey      73211820.18422
                  
                  post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
                  post.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
                  post.getParams().setParameter("http.protocol.single-cookie-header", true);
                  
                  NameValuePair[] params = new NameValuePair[]{
                              new NameValuePair("password", "13476045697"),
                              new NameValuePair("product", "t"),
                              new NameValuePair("sub", "登录"),
                              new NameValuePair("type", "1"),
                              //callback_url 可加可不加,不加则默认跳转到应用设置上的callback_url
//                            new NameValuePair("url", "http://api.t.163.com/oauth/authenticate?oauth_token="+requestToken.getToken()+"&oauth_callback="+callback_url+"&authorize=1"),
                              new NameValuePair("url", "http://api.t.163.com/oauth/authenticate?oauth_token="+requestToken.getToken()+"&oauth_callback=oob&authorize=1"),
                              new NameValuePair("username", "zzzzzzzzz@163.com"),
                  };
                  
                  post.setRequestBody(params);
                  
                  int status = httpClient.executeMethod(post);
                  String response = post.getResponseBodyAsString();
//                System.out.println(status+" "+response);
                  
//                String[] redirectUrls = StringUtils.substringsBetween(response, "url = \"", "\";");
                  String redirectUrl = StringUtils.substringBetween(response, "url = \"", "\";");
                  
//                printHeaders(post);
//                printCookies(httpClient);
                  
                  
//                redirectUrl = URLDecoder.decode(redirectUrl, "utf-8");
                  GetMethod get = new GetMethod(redirectUrl);
                  status = httpClient.executeMethod(get);
                  
                  response = get.getResponseBodyAsString();
                  redirectUrl = StringUtils.substringBetween(response, "url = \"", "\";");
//                System.out.println(status+" "+response);
                  
//                printHeaders(get);
//                printCookies(httpClient);
                  
                  
                  get = new GetMethod(redirectUrl);
                  status = httpClient.executeMethod(get);
                  
                  response = get.getResponseBodyAsString();
//                System.out.println(status+" "+response);
                  
//                printHeaders(get);
//                printCookies(httpClient);
                  
                  
                  AccessToken accessToken= tblog.getOAuthAccessToken(requestToken);
                  User user  = tblog.verifyCredentials();
                  
                  accessToken.setScreenName(user.getScreenName());
                  accessToken.setUserId(user.getId());
                  
                  System.out.println("网易 "+user.getScreenName()+" 授权码:");
                  System.out.println(accessToken.getToken());
                  System.out.println(accessToken.getTokenSecret());
            } catch (HttpException e)
            {
                  e.printStackTrace();
            } catch (TBlogException e)
            {
                  e.printStackTrace();
            } catch (IOException e)
            {
                  e.printStackTrace();
            }
            
//          String tokenSecret = requestToken.getTokenSecret();
//          
//          CrawlHttpClientHelper crawlHttpClientHelper = new CrawlHttpClientHelper();
//          HttpClient httpClient = new HttpClient();
//          HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();
//          params.setConnectionTimeout(3000);
//          params.setSoTimeout(6000);
            
            
 
      }
      
      public static void printCookies(HttpClient httpClient){
            Cookie[] cookies = httpClient.getState().getCookies();
        if (cookies.length == 0) {
           System.out.println("None");   
        } else {
           for (int i = 0; i < cookies.length; i++) {
              System.out.println(cookies[i].toString());   
           }
        }
            
      }
      public static void printHeaders(HttpMethodBase base){
            System.out.println("header: ");
            for (Header header : base.getResponseHeaders()) {
//                headers.put(header.getName().toLowerCase(), header.getValue());
        System.out.println(header.getName()
                    + ":" + header.getValue());
        }
            
      }
posted @ 2014-11-08 11:36  程序员斯文  阅读(1010)  评论(0编辑  收藏  举报