Android HttpClient自己主动登陆discuz论坛!

你登陆论坛的时候,我们先看看浏览器干了什么事儿:

用Firefox打开HiPda 的登陆页面,输入用户名和password,点登陆。

以下是通过firebug插件获取的数据:


能够看到浏览器这个http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1网址发了一个POST请求

看一下它POST的參数是什么:


能够看到一共同拥有7个參数:

第一个cookietime=259200,这个是固定的,直接传这个值过去即可;

第二个formhash是discuz论坛的一个设置,值在当前页面的源代码里。

比方我们看一下网页的源代码,搜一下formhash跟这里的formhash是不是一样的:


刚好是一样的。

第三个值loginfield是固定的,等于username;

第四个是你输入法password。

第五个是安全提问的编号,因为我们没有选安全提问的问题,所以编号为0;

第六个referer。直接输进去这个即可;

第七个是你的用户名。


以下我们用代码实现自己主动登录。

首先通过上面的分析,首先须要formhash的值。这个我们能够通过HttpGet得到网页的源代码。把formhash解析出来。

                HttpClient httpClient = new DefaultHttpClient();
                //得到网页的formhash值。用Jsoup解析出来
                HttpGet httpGet = new HttpGet("http://www.hi-pda.com/forum/logging.php?

action=login"); try{ HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); String s = EntityUtils.toString(httpEntity,"GBK"); Element formhash_Element = Jsoup.parse(s).select("input[name=formhash]").first(); formhash = formhash_Element.attr("value"); System.out.println(formhash); } catch(Exception e ){ }

以下我们就能够登陆了。用HttpPost:

                HttpPost httpPost=new HttpPost("http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1");
                List<NameValuePair> params=new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("formhash",formhash));
                params.add(new BasicNameValuePair("loginfield","username"));
                params.add(new BasicNameValuePair("password","******"));
                params.add(new BasicNameValuePair("questionid","0"));
                params.add(new BasicNameValuePair("referer","http://www.hi-pda.com/forum/index.php"));
                params.add(new BasicNameValuePair("username","******"));
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(params, "GBK"));

                    HttpResponse response=httpClient.execute(httpPost);
                    HttpEntity entity=response.getEntity();
                    String ans=EntityUtils.toString(entity);


                }catch (Exception e){

                }
如今我们已经登陆成功了,仅仅要用同一个HttpClient对象,就会一直显示登录状态。比方我们用这个httpClient打开一下D版试一下:

                HttpGet getHome = new HttpGet("http://www.hi-pda.com/forum/index.php");
                try{
                    httpClient.execute(getHome);
                }catch (Exception e){

                }
                HttpGet getD=new HttpGet("http://www.hi-pda.com/forum/forumdisplay.php?fid=2");
                try {
                    HttpResponse responseD = httpClient.execute(getD);
                    HttpEntity entityD=responseD.getEntity();
                    String str=EntityUtils.toString(entityD,"GBK");
                    System.out.println(str);
                }catch (Exception e){

                }

能够看到显示的是已登陆的D版的内容。



转载于:https://www.cnblogs.com/mqxnongmin/p/10584300.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值