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

      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. 记一次HTTPClient模拟登录获取Cookie的开发历程

    记一次HTTPClient模拟登录获取Cookie的开发历程 环境: ​ springboot : 2.7 ​ jdk: 1.8 ​ httpClient : 4.5.13 设计方案 ​ 通过新建一个 ...

  2. .Net HttpClient 模拟登录微信公众平台发送消息

    1.模拟登录 public WeiXinRetInfo ExecLogin(string name, string pass) { CookieContainer cc = new CookieCon ...

  3. 使用python模拟登录网易邮箱网站

    环境要求 python 3.6 chromedriver.exe 文件 人工登录 人工登录某个网站,首先需要用浏览器打开登录页面,然后在输入框中输入对应的账号和密码,最后点击登录,以下使用代码模拟以上 ...

  4. HttpClient 模拟登录搜狐微博

    http://mengyang.iteye.com/blog/575671 第一次遇到一个这样的问题,"PKIX path building failed" 异常   详解异常:  ...

  5. HttpClient + Jsoup模拟登录教务处并获取课表

    1.概述 最近想做一个校园助手类的APP,由于第一次做,所以打算先把每个功能单独实现,防止乱了阵脚.利用教务处登录获取课表和成绩等是一个基本功能,所以以获取课表为例实现了这个功能.完整代码点这里,尝试 ...

  6. Java通过httpclient获取cookie模拟登录

    package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...

  7. Java语言使用HttpClient模拟浏览器登录

    使用HttpClient来模拟浏览器登录网站,然后可以进行操作,比如发布信息等 第一步:获取实际的post网址,(不考虑复杂情况下) 1.需要使用到firefox的httpfox插件,httpfox中 ...

  8. scrapy模拟登录微博

    http://blog.csdn.net/pipisorry/article/details/47008981 这篇文章是介绍使用scrapy模拟登录微博,并爬取微博相关内容.关于登录流程为嘛如此设置 ...

  9. java 模拟登录新浪微博(通过cookie)

    这几天一直在研究新浪微博的爬虫,发现爬取微博的数据首先要登录.本来打算是通过账号和密码模拟浏览器登录.但是现在微博的登录机制比较复杂.通过账号密码还没有登录成功QAQ.所以就先记录下,通过cookie ...

随机推荐

  1. 事务ACID特性,其中I代表隔离性(Isolation)。

    事务ACID特性,其中I代表隔离性(Isolation). 什么是事务的隔离性? 隔离性是指,多个用户的并发事务访问同一个数据库时,一个用户的事务不应该被其他用户的事务干扰,多个并发事务之间要相互隔离 ...

  2. 15个变态的Google面试题以及答案

    在当前经济形势不景气的情况下,谷歌招聘新员工是一件令人振奋的事,特别是对那些在当前金融风暴中渴望找到安全港的年轻经理们和软件开发商们来说是个好消息. 不过,也不要高兴太早,谷歌在招聘新员工时,更加青睐 ...

  3. live-server 快速搭建静态服务

    作为切图仔呢,平时都是在修改页面样式.展示后台返回的数据.调试js效果,这时候呢就需要搭一个服务器以便我们调试.平时我一般用的都是wamp,在www目录下新建项目然后开始撸代码,不过有时候呢不太方便, ...

  4. Activity之启动模式

    在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. Ac ...

  5. 《手把手教你学C语言》学习笔记(9)--- 程序的选择控制

    C语言是面向过程编程语言的主要代表,其特征就是严格控制程序的执行语句顺序,因此,C程序的主要结构控制就是顺序控制,以main函数为入口函数,根据控制,一条一条地执行语句.由于实际需求是很复杂的,只用顺 ...

  6. Python学习杂记_11_函数(一)

    函数也叫方法,就是把实现某种功能的一组代码封装起来,当你需要这个功能时直接调用函数即可. 定义函数:定义函数时要注意 “def”关键字,“:”,“函数体缩进”:用“return”使函数有具体返回值,没 ...

  7. AC日记——Car的旅行路线 洛谷 P1027

    Car的旅行路线 思路: 这题不难,就是有点恶心: 而且,请认真读题目(就是题目卡死劳资): 来,上代码: #include <cmath> #include <cstdio> ...

  8. Codeforces 583 DIV2 GCD Table 贪心

    原题链接:http://codeforces.com/problemset/problem/583/C 题意: 大概就是给你个gcd表,让你还原整个序列. 题解: 由$GCD(a,a)=a$,我们知道 ...

  9. luogu P1489 猫狗大战

    题目描述 新一年度的猫狗大战通过SC(星际争霸)这款经典的游戏来较量,野猫和飞狗这对冤家为此已经准备好久了,为了使战争更有难度和戏剧性,双方约定只能选择Terran(人族)并且只能造机枪兵. 比赛开始 ...

  10. 转:浅析Collections.sort

    浅析Collections.sort 问题引入   在之前的一次Java上机实习中,老师布置了一道很简单的题: 从控制台输入10个整数,对它们进行升序排序并输出.   考虑到只有10个数,需要比较的次 ...