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

      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_HANDLERnew 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());
        }
            
      }

HttpClient 模拟登录网易微博相关推荐

  1. python模拟登录163邮箱_Python实现模拟登录网易邮箱的方法示例

    本文实例讲述了Python实现模拟登录网易邮箱的方法.分享给大家供大家参考,具体如下: #coding:utf-8 import urllib2,urllib import cookielib fro ...

  2. python模拟登录网易邮箱

    python模拟登录网易邮箱 #coding:utf-8 import urllib2,urllib import cookielib from bs4 import BeautifulSoup#设置 ...

  3. python模拟登录163邮箱_python模拟登录网易邮箱-阿里云开发者社区

    python模拟登录网易邮箱 #coding:utf-8 import urllib2,urllib import cookielib from bs4 import BeautifulSoup #设 ...

  4. JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解

    目录 前言 分析 代码实现 第一次GET POST登录 第二次Get 第三次GET 第四次GET 第五次GET 测试 完整代码 前言 最近在做一个APP,需要获取我们学校--武汉纺织大学皇家停水断电断 ...

  5. HttpClient模拟登录、HtmlUnit模拟登录,jsoup爬取登录后的内容(不带验证码)

    一.HttpClient模拟登录是通过post或get请求,登录后抓取的是静态页面,动态页面使用HtmlUnit public static void main(String[] args) thro ...

  6. Java使用HttpClient模拟登录微博

    前言 前几天刚刚接触了Java这边的关于HTTP的一个工具包-HttpClient , 那么就想借此机会练练手, 用这个工具进行对微博的模拟登录, 简单的获取一下微博的数据 , 但是大家可以不必执着于 ...

  7. HttpClient 模拟登录手机版新浪微博

    手机版微博单点登录的URL为:http://3g.sina.com.cn/prog/wapsite/sso/login.php 我们先来看一下这个网页源代码: <html><head ...

  8. HttpClient 模拟登录Web版新浪微博

    上篇介绍了如何模拟登录手机版微博,过程还是比较简单的,没有设计到复杂的加密部分. 登录Web版微博的过程还是一样的,只不过这次需要提交的数据多一点. public static Cookie[] ge ...

  9. java httpclient 模拟登录

    新版HttpClient4.2与之前的3.x版本有了很大变化,建议从http://hc.apache.org/处以得到最新的信息. 关于HttpCore与HttpClient:HttpCore是位于H ...

最新文章

  1. 2013年7月12日“修复 Migration 测试发现的 Bug”
  2. HDU-3038-How Many Answers Are Wrong
  3. 在电路设计中,这7个接口类型太重要了,我难道不该学学么!
  4. leetcode669. 修剪二叉搜索树
  5. Java HashSet和HashMap源码剖析
  6. Java面向对象(18)--final关键字使用
  7. 从遇见到信任 | Apache Dubbo 的毕业之旅
  8. go get如何删除_在Go使用Sqlite和Accsee
  9. Python+pandas处理Excel文件中的超市营业额数据
  10. [转].net cookie版购物车
  11. jQuery 文档操作方法 (四)
  12. ArcGis拓扑——规则、概念与要点
  13. python中的减号_我的python把减号和破折号混淆了
  14. 国企,私企与外企利弊通观--关键时刻给应届毕业生及时点拨(二)
  15. word2010公式编辑器 格式设置
  16. 电子计算机是采用什么进制法,计算机内部使用什么进制
  17. Excel 调用百度翻译API进行翻译
  18. 解决error while accessing a target resource. resource is perhaps not available or a wrong access was
  19. CE修改植物大战僵尸豌豆射手的射速
  20. JVM(七) - Jvm内存模型

热门文章

  1. 后端服务接口都在测试什么?怎么测?
  2. 技术提升为管理,最重要的能力是什么?
  3. 测试人如何高效地设计自动化测试框架?
  4. C# 打开指定文件夹下的excel
  5. stc15w4k32s4管脚图_STC15W4K32S4系列
  6. Least-Squares Fitting of Two 3-D Point Sets
  7. DXUT实战3:HLSL(withEffect)+D3D9+DXUT(june_2010) . .
  8. 自定义训练中使用Tensorboard
  9. 神经网络填充(padding)
  10. 浅谈抖音下拉词框优化推广的优势