资源描述:
《httpclient模拟登录web版新浪微博》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、HttpClient模拟登录Web版新浪微博上篇介绍了如何模拟登录手机版微博,过程还是比较简单的,没有设计到复杂的加密部分。登录Web版微博的过程还是一样的,只不过这次需要提交的数据多一点。publicstaticCookie[]getWCookies(Stringusername,Stringpassword)throwsHttpException,IOException{HttpClientclient=null;PostMethodpost=null;GetMethodget=null;t
2、ry{client=newHttpClient();client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);post=newPostMethod("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.16)");Stringdata=getServerTime();Stringnonce=makeNonce(6);NameValuePai
3、r[]nvps=newNameValuePair[]{newNameValuePair("entry","weibo"),newNameValuePair("gateway","1"),newNameValuePair("from",""),newNameValuePair("savestate","7"),newNameValuePair("useticket","1"),newNameValuePair("ssosimplelogin","1"),newNameValuePair("vsnf
4、","1"),newNameValuePair("vsnval",""),newNameValuePair("su",encodeAccount(username)),newNameValuePair("service","miniblog"),newNameValuePair("servertime",data),newNameValuePair("nonce",nonce),newNameValuePair("pwencode","wsse"),newNameValuePair("sp",n
5、ewSinaSSOEncoder().encode(password,data,nonce)),newNameValuePair("encoding","UTF-8"),newNameValuePair("returntype","META"),newNameValuePair("url","http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack")};po
6、st.setRequestBody(nvps);client.executeMethod(post);Stringurl=post.getResponseBodyAsString().substring(post.getResponseBodyAsString().indexOf("http://weibo.com/ajaxlogin.php?"),post.getResponseBodyAsString().indexOf("code=0")+6);get=newGetMethod(url);
7、client.executeMethod(get);}catch(IOExceptione){e.printStackTrace();}finally{get.abort();post.abort();}returnclient.getState().getCookies();}其中密码部分进行了加密,加密的算法在网页的js文件里,网上有人把它改成了Java代码。下面是SinaSSoEncoder类:publicclassSinaSSOEncoder{privatebooleani=false;
8、privateintg=8;publicSinaSSOEncoder(){}publicStringencode(Stringpsw,Stringservertime,Stringnonce){Stringpassword;password=hex_sha1(""+hex_sha1(hex_sha1(psw))+servertime+nonce);returnpassword;}privateStringhex_sha1(Stringj){returnh(b(f(j,j.length()*g),