新浪微博模拟登陆及常用功能实现

来源:互联网 发布:苏曼莎捏脸数据 编辑:程序博客网 时间:2024/06/05 04:41

在实现功能的过程中多次使用WireShark和httpFox进行捕包分析。


实现的功能:

1.      网页微博的模拟登陆

2.      发送微博,转发微博,评论微博,删除微博

 

用到的jar包:

httpcomponents-client-4.5

jsonrpc-1.0.jar(对json数据进行解析)

commons-codec-1.8(主要是一些加密函数)

 

过程中主要遇到的三个问题:

 

在wireshark里面捕到的包get方法链接

/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=MzkzNTA0MTQ0JTQwcXEuY29t&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=143433176559

ssologin.js(v1.4.18)不能直接用()而应该换成括号应该换成%28%28

要获取数据应该用的链接形式如下

http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.s%28v1.4.18%29&_=1434337083538

 

 

最开始发表评论一直失败,是由于HttpClient4.x有一定能力处理cookie和header,以为可以自动处理header信息,导致发表一直失败,经过分析知道是由于header信息不对,随后对header进行了手动添加,发布微博函数测试成功

 

在获取发表微博返回信息的时候,由于返回的数据是经过gzip压缩的,所以要对httpClient进行设置,以便使其可以支持对返回的压缩数据进行解压的能力,否则获取到的数据会出现乱码。具体设置方法如下

 

//返回的数据是经过gzip压缩的,这里需要对HttpClient进行设置,使之能够支持对gzip进行解压,否则获取到的数据会出现乱码

                            GlobalValues.httpClient.addRequestInterceptor(newHttpRequestInterceptorImplementation());

                           

                            GlobalValues.httpClient.addResponseInterceptor(newHttpResponseInterceptor(){

                                   publicvoidprocess(HttpResponseresponse, HttpContextcontext)

                                                throwsHttpException, IOException {

                                         

                                         HttpEntityentity =response.getEntity(); 

                                    Headerceheader = entity.getContentEncoding(); 

                                   if (ceheader !=null){ 

                                        HeaderElement[]codecs = ceheader.getElements(); 

                                       for (inti = 0;i < codecs.length;i++){ 

                                           if (codecs[i].getName().equalsIgnoreCase("gzip")){ 

                                                response.setEntity( 

                                                       newGzipDecompressingEntity(response.getEntity()));  

                                               return

                                            } 

                                        } 

                                    } 

                                   }

                                  

                            });

privatestaticclass HttpRequestInterceptorImplementationimplements

                     HttpRequestInterceptor {

              publicvoidprocess(HttpRequestrequest, HttpContextcontext)

                            throwsHttpException, IOException {

                     if (!request.containsHeader("Accept-Encoding")) { 

                         request.addHeader("Accept-Encoding","gzip"); 

                     } 

              }

       }

 

发布微博和转发微博要有一定的时间差,要不是会发布不成功

{"code":"100001","msg":"\u5fae\u535a\u53d1\u7684\u592a\u591a\u5566\uff0c\u4f11\u606f\u4e00\u4f1a\u518d\u53d1\u5566\uff01(100001)","data":{}}

\u5fae\u535a\u53d1\u7684\u592a\u591a\u5566\uff0c\u4f11\u606f\u4e00\u4f1a\u518d\u53d1\u5566\uff01是UTF-8编码,翻译过来是“微博发的太多啦,休息一会再发啦!”,这里解决方法是Thread.sleep(time),让发完微博之后停一会在转发。

 

 

 

遗留问题:

微博点赞功能尚未实现

一些用户信息的获取,需要对页面进行分析,由于新浪的页面比较奇葩,是在js脚本里面,再由js生成页面,获取entity不是纯正的html格式,而是UTF-8格式的String类型,解析需要时间,在以后的时间里面应该会补全相应的功能。

 

 

0 0
原创粉丝点击