java 后台模拟登陆新浪微博

本文详细解析了使用新浪通行证登录新浪微博的过程,包括构建POST请求、处理JSON响应信息、构建GET请求以及获取有效的微博Cookies。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.需要用到的包:

commons-codec-1.4.jar

commons-httpclient-3.0.1.jar

commons-logging-1.1.1.jar

二.分析新浪通行证登录页面

登录页面:      

       新浪微博的登录页面中,我发现密码在传过去之前已经被js加密过,而js中加密方法很难找,而新浪通行证中的密码并没有加密,所以选择通过新浪通行证登录

地址:http://login.sina.com.cn/signup/signin.php?entry=sso

简要说明登录过程:

         1.将用户信息提交到https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1425914562105中,

火狐浏览器中测出,它返回了一部分cookie和一个json格式的响应信息,但这部分cookie是不能作用在微博中的,因为他们和微博的主机、作用域不同

        2.用js处理返回的json信息,构建一个get请求,其参数主要由json中的信息构成

        3.返回微博的cookie

        4.用这部分cookie可以获取微博的信息

       

三.具体登录过程:

      1.将用户名、密码等信息通过post请求传入https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1425914562105中,获得许多cookie和一个json格式的响应体。

                

    public static void main(String[] args) {
        HttpClient client=new HttpClient();
        NameValuePair pairs[]={
            new NameValuePair("cdult","3"),
            new NameValuePair("encoding","UTF-8"),
            new NameValuePair("from",null),
            new NameValuePair("gateway","1"),
            new NameValuePair("prelt","0"),
            new NameValuePair("pagerefer","http://login.sina.com/sso/login.php"),
            new NameValuePair("returntype","TEXT"),
            new NameValuePair("savestate","30"),
            new NameValuePair("service","sso"),
            new NameValuePair("sp","密码"),
            new NameValuePair("sr","1366*778"),
            new NameValuePair("su",getBASE64("用户名")),
            new NameValuePair("useticket","0"),
            new NameValuePair("vsnf","1")

        };

        HttpMethod method=getPostMethod("https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_="+new Date().getTime(), null, pairs);
        client.getParams().setContentCharset("GBK");
        try {
            client.executeMethod(method);
//        System.out.println(method.getResponseBodyAsString());
            InputStream stream = method.getResponseBodyAsStream();  
                   
                 BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));  
                 StringBuffer buf = new StringBuffer();  
                 String line;  
                 while (null != (line = br.readLine())) {  
                     buf.append(line).append("\n");  
                 }  
                 System.out.println(buf.toString());  
             method.releaseConnection();  
             
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
    }
    public static HttpMethod getPostMethod(String url,Header headers[],NameValuePair pairs[]){
        PostMethod method=new PostMethod(url);
        if(headers!=null){
            for(Header header:headers){
                method.setRequestHeader(header);
            }
        }
        method.setRequestBody(pairs);
        return method;
    }

            上述代码中,最重要的部分是post参数中returntype,“TEXT”因为在ie中如果接收到json或者text格式的数据会自动提示下载,所以ie中这部分参数对应的value是“JFrame”格式的,由于拿不到我想要的东西,其登录过程我没有分析。

            经运行,我们得到下述json格式的代码:

{"retcode":"0","uid":"2136324567",
"nick":"\u7528\u62372136324567",
"crossDomainUrlList":
["https:\/\/passport.weibo.com\/wbsso\/login?ticket=ST-MjEzNjMyNDU2Nw%3D%3D-1425964519-ja-B24DC4AB2894692B3B38928C36A11009&ssosavestate=1457500519",
"https:\/\/crosdom.weicaifu.com\/sso\/crosdom?action=login&savestate=1457500519",
"http:\/\/passport.weibo.cn\/sso\/crossdomain?action=login&savestate=1"]}

            2.      根据返回json构建get请求例如

https://passport.weibo.com/wbsso/login?ticket=ST-MjEzNjMyNDU2Nw%3D%3D-1425965391-ja-BB03424873E09D9B8E6FF8BA8763304F&ssosavestate=1457501391&callback=sinaSSOController.doCrossDomainCallBack&scriptId=ssoscript0&client=ssologin.js(v1.4.15)&_=1425965215809

                这里我理解的是新浪会根据我们提交的信息给我们一张票去访问微博,这些参数中,ticket和ssosavestate是json中给的_=后面是new Date().getTime(),其它固定,更新上面代码,全部代码如下

private static String username="xxxxx";
    private    static String password="xxxxx";
    public static void main(String[] args) {
        String ticket="";
        String ssosavestate="";
        HttpClient client=new HttpClient();
        NameValuePair pairs[]={
            new NameValuePair("cdult","3"),
            new NameValuePair("encoding","UTF-8"),
            new NameValuePair("from",null),
            new NameValuePair("gateway","1"),
            new NameValuePair("prelt","0"),
            new NameValuePair("pagerefer","http://login.sina.com/sso/login.php"),
            new NameValuePair("returntype","TEXT"),
            new NameValuePair("savestate","30"),
            new NameValuePair("service","sso"),
            new NameValuePair("sp","wobuaini50"),
            new NameValuePair("sr","1366*778"),
            new NameValuePair("su",getBASE64(username)),
            new NameValuePair("useticket","0"),
            new NameValuePair("vsnf","1")

        };

        HttpMethod method=getPostMethod("https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_="+new Date().getTime(), null, pairs);
        client.getParams().setContentCharset("GBK");
        try {
            client.executeMethod(method);
            String temp=method.getResponseBodyAsString();
            if(temp.contains("ticket")){
                ticket=temp.substring(temp.indexOf("ticket="),temp.indexOf("&"));
                ssosavestate=temp.substring(temp.indexOf("ssosavestate"),temp.indexOf("\"", temp.indexOf("ssosavestate")));
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        String url2="https://passport.weibo.com/wbsso/login?"+ticket+"&"+ssosavestate+"&callback=sinaSSOController.doCrossDomainCallBack&scriptId=ssoscript0&client=ssologin.js(v1.4.15)&_="+new Date().getTime();
        String cookieStr="";
        Header[] headers=null;
        HttpClient client2=new HttpClient();
        GetMethod method2=new GetMethod(url2);
        try {
            client2.executeMethod(method2);
            headers=method2.getResponseHeaders();
            method2.releaseConnection();
        } catch (HttpException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Cookie[] cookies=client2.getState().getCookies();
        for (int i = 0; i < cookies.length; i++) {
            String temp=cookies[i].getName();
            if(temp.equals("SUE")||temp.equals("SUS")||temp.equals("SUP")){
                cookieStr+=cookies[i].getName()+"="+cookies[i].getValue()+";";
            }
        }
        cookieStr+="un="+username;
        System.out.println(cookieStr);
    }
    public static HttpMethod getPostMethod(String url,Header headers[],NameValuePair pairs[]){
        PostMethod method=new PostMethod(url);
        if(headers!=null){
            for(Header header:headers){
                method.setRequestHeader(header);
            }
        }
        method.setRequestBody(pairs);
        return method;
    }
    public HttpMethod getGetMethod(String url,Header headers[]){
        HttpMethod method=new GetMethod(url);
        if(headers!=null){
            for(Header header:headers){
                method.setRequestHeader(header);
            }
        }
        return method;
    }
    @SuppressWarnings("restriction")
    public static String getBASE64(String s) {
        if (s == null) return null;
        return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
        }

返回结果是:

SUS=SID-2136324567-1425965890-JA-6hy0f-a36939ccbd9b8192ecfa6b7dd7fd4122;SUE=es%3D72da1b83721474c9606bbec7bc8080b7%26ev%3Dv1%26es2%3Da0aa1dcd967281462c90f257a9daed9c%26rs0%3DgD8KqazZqR7q%252F3ytvcgRcNWY1frB7gvFQRxN3w0XLhZ3BvZy5tMWHZ58BMpG1X6a%252FisOZ0UPUCyuCdJOv6wfUfp1k1f9AadexOQoOAVFbkqr3LTiLI6u%252FkHe6fezN842YuY0w110jhU0Ls4vFOJvgOksPp4vpi5HOGKhqbmyGWQ%253D%26rv%3D0;SUP=cv%3D1%26bt%3D1425965890%26et%3D1426052290%26d%3Dc909%26i%3D4122%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D0%26st%3D0%26uid%3D2136324567%26name%3D15201315239%2540sina.cn%26nick%3D%25E7%2594%25A8%25E6%2588%25B72136324567%26fmp%3D%26lcp%3D2014-04-29%252018%253A44%253A36;un=yourusername

这时我们只需要在我们的请求头中 setRequestHeader("Cookie", cookieStr);就可以访问微博的各个页面了

举例部分代码如下,可以放到上述代码的main中

HttpClient client3=new HttpClient();
        GetMethod method3=new GetMethod("http://weibo.com/friends?leftnav=1&wvr=6&isfriends=1&step=2");
        method3.setRequestHeader("Cookie", cookieStr);
        try {
            client3.executeMethod(method3);
            System.out.println(method3.getResponseBodyAsString());
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

测试成功~

代码不合理的地方还请指教

            


     










转载于:https://my.oschina.net/u/2323537/blog/384738

内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值