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

      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. Codeforces Round #316 (Div. 2) B 贪心

    B. Simple Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. javasript深度拷贝

    1 将json数据转换为字符串形式 var j={"name":"daenerys targaryen","dragon":"{& ...

  3. MAC电脑安装Mysql服务器和Navicat for mysql客户端

    1.下载链接 Navicat for mysql客户端 链接: https://pan.baidu.com/s/1dGbzgbR 密码: i43g Mysql服务器 链接: https://pan.b ...

  4. NS5S1153 切換器

    昨天在研讀 NS5S1153 的 spec, 發現有一個詞 很陌生 DPDT, 原來是 double pole double throw 的縮寫,雙軸雙切, 更詳細的解釋可以看這個博客的另一篇 &qu ...

  5. linux私房菜-读书笔记

    第零章:计算机概论 计算机:接受用户输入指令和数据,经过中央处理器的数据和逻辑单元运算处理器处理后,以产生或存储成有用的信息. 计算机硬件的五大单元:输入单元.输出单元.CPU内部控制单元.算术逻辑单 ...

  6. cisco packet 实验教程(二)

    06. 三层交换机实现VLAN间路由 技术原理 1)三层交换机是带有三层路由功能的交换机,也就是这台交换机的端口既有三层路由功能,也具有二层交换功能.三层交换机端口默认为二层口,如果需要启用三层功能就 ...

  7. hdu 2654(欧拉函数)

    Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. C# 数组与 list 互相转换案例

    在项目中用到了 随手分享下 记得点赞呦! 1,从System.String[]转到List<System.String>System.String[] str={"str&quo ...

  9. JS-JavaScript String 对象-string对象方法1:fromCharCode()、charCodeAt()

    1.fromCharCode(): 可接受一个指定的 Unicode 值,然后返回一个字符串. 1). 语法:String.fromCharCode(n1, n2, ..., nX)  (n1, n2 ...

  10. 【IDEA】(3)---非常实用提高开发效率和水平的插件

    IDEA(3)-Mac中IDEA插件 IDEA提供了许多很实用的插件,能够大大提高开发效率和开发水平,这里列举几个很实用的插件. 说明:这边的IDEA是MAC系统. 一.插件管理界面简介 1.界面位置 ...