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

      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. 座位安排(seat)

    座位安排(seat) 题目描述 费了一番口舌,wfj_2048终于成功地说服了n∗mn∗m个妹子来陪自己看电影. 为了这次声势浩大,wfj_2048包下了一座有n∗mn∗m个座位的电影院. (wfj_ ...

  2. java课后作业-4

    一.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. public class suiji { private static final int N = 200; private ...

  3. Hyperledger Fabric 环境配置

    简单说一下 Hyperledger Fabric的配置 1.第一步,安装curl brew install curl 注:没有brew的自行百度(mac) 2. 安装Docker 下载并安装Docke ...

  4. 将扁平化的JSON属性转换为嵌套的JSON

    需要将如下JSON {"a":"a","b":"b","c.e":"e",&qu ...

  5. linux之tr

    通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其)简化的变体:它可以用一个字符来替换另一个字符,或者可以完全除去一些字符.您也可以用它来除去重复 ...

  6. 5种你未必知道的JS和CSS交互的方法

    随着浏览器不断的升级改进,CSS和JavaScript之间的界限越来越模糊.本来它们是负责着完全不同的功能,但最终,它们都属于网页前端技术,它们需要相互密切的合作.我们的网页中都有.js文件和.css ...

  7. linux中的vi编辑器(一)

    1.在linux中如果两个用户都在打开一个文件进行编辑,那么文件最后将以最后保存的版本为主. 2.vi仅仅是一个文本编辑器,相当于windows中的记事本,vi的工作模式, 命令模式:在该模式下,在键 ...

  8. 关于platform_device和platform_driver的匹配【转】

    转自:http://blog.csdn.net/dfysy/article/details/5959451 版权声明:本文为博主原创文章,未经博主允许不得转载. 说句老实话,我不太喜欢现在Linux ...

  9. Android build code command

    make bootimage -j8 make systemimage -j8

  10. Android Timer 的 schedule()方法定时循环切换图片

    void java.util.Timer.schedule(TimerTask task, long delay, long period)第一个参数,是 TimerTask 类,在包:import ...