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; [email protected]|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 [email protected]

//                _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", "[email protected]"),

};

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

}

}

时间: 2024-07-30 22:52:00

HttpClient 模拟登录网易微博的相关文章

C# 中使用System.Net.Http.HttpClient 模拟登录博客园 (GET/POST)

一. System.Net.Http.HttpClient简介 System.Net.Http 是微软.net4.5中推出的HTTP 应用程序的编程接口, 微软称之为“现代化的 HTTP 编程接口”, 主要提供如下内容: 1. 用户通过 HTTP 使用现代化的 Web Service 的客户端组件: 2. 能够同时在客户端与服务端同时使用的 HTTP 组件(比如处理 HTTP 标头和消息), 为客户端和服务端提供一致的编程模型. 个人看来是抄袭apache http client ,目前网上用的

httpclient 模拟登录 及线程安全

//登录csdn//        String uri = "https://passport.csdn.net/account/login";//        String html = HttpUtil.DownHtml(uri);        //        <input type="hidden" name="lt" value="LT-207426-moK0sGnfCa9aqijJKeLYhFDYiEe2id&

使用HttpClient模拟登录人人网+发状态+日志+分享+留言

事先使用HttpAnalyzer对人人网抓包观察,得到相关事件的数据.然后使用HttpClient模拟http请求相关操作.以下代码是今天刚测试过.欢迎新手拿去玩玩.注意:首先要在工程里导入httpclient的包,在apache官网上应该有的下载: http://hc.apache.org/downloads.cgi,目前是4.0+版本:其次记得将主函数里的账号和密码改为自己的,即可完成. import java.io.IOException; import java.io.Unsupport

HttpClient 模拟登录搜狐微博

http://mengyang.iteye.com/blog/575671 第一次遇到一个这样的问题,"PKIX path building failed" 异常 详解异常: http://www.cnblogs.com/aurawing/articles/1899217.html 大概意思是,证书出问题了,服务器不信任我们自己创建的证书. 两种解决方法 1.请求时添加代码忽略证书信任问题 我用httpClient,简单明了的两句代码(支持SSL,也就是以https://开头的,一般是

HttpClient 4.1.3 初学篇---使用Get和Post模拟登录简单页面(分别带参数)

最近需要解决的问题需要用到Httpclient,模拟登陆网站!成功之后就可以用代码的方式获取网站内容或者发送请求,类似网络爬虫. 但是在网上找了好多篇Blog,发现每一片的写法都不一样,也纠结了些时间,很纳闷,才发现Httpclient版本不一样...现在在这里说明我使用的版本是HttpClient 4.1.3,我已上传下载 看了些Blog,发现直接访问大型的网站不太容易,于是就自己写了小的站点,只有一个Servlet,来接受参数(用户名和密码)就ok了! 这个Servlet就只有get与pos

android/java模拟登录正方教务系统

最近闲来无事,打算开始写博客,也算是对自己知识的一个总结.本篇将讲解如何使用HttpClient模拟登录正方教务系统. 需要使用道德jar包:HttpClient,Jsoup.(下载jar包) 本次模拟登录的成都大学的教务系统,其他学校的教务系统,可参照本文给出的流程和代码进行修改并测试. 基本流程: 1).使用谷歌浏览器打开教务系统首页,并打开浏览器开发者工具记录浏览过程,然后正常登录并浏览自己的课表,成绩等信息. 2).下载jar包,将jar引用到自己需要的项目中,可创建一个新的工具类. 3

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

1.概述 最近想做一个校园助手类的APP,由于第一次做,所以打算先把每个功能单独实现,防止乱了阵脚.利用教务处登录获取课表和成绩等是一个基本功能,所以以获取课表为例实现了这个功能.完整代码点这里,尝试了好几次的,所以写的比较乱. 2.涉及的关键知识 首先,明确获取课表的流程:其实,获取课表就是让手机模拟浏览器,给服务器传去账号.密码,然后服务器会返回cookies(不懂自行百度),利用cookie就可以穿梭自如了,比如查课表.但是,浏览器登录时,返回的html文件浏览器是会自动解析成网页展现在我

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

使用HttpClient来模拟浏览器登录网站,然后可以进行操作,比如发布信息等 第一步:获取实际的post网址,(不考虑复杂情况下) 1.需要使用到firefox的httpfox插件,httpfox中clear一下,然后start开始捕获 2.切换回网页的登录页面,开始输入自己的账号密码登录,登录成功后切回httpfox中stop,查看最近的post方法中包含的Post Data数据,和此post方法的url网址, 3.这样就得到了模拟登录时需要Post的数据参数(Parameter)和值(Va

Java通过httpclient获取cookie模拟登录

package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpc