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

编程入门 行业动态 更新时间:2024-10-25 22:25:11

Android HttpClient自己<a href=https://www.elefans.com/category/jswz/34/1768396.html style=主动登陆discuz论坛!"/>

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

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

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

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


能够看到浏览器这个.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(".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(".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",".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(".php");try{httpClient.execute(getHome);}catch (Exception e){}HttpGet getD=new HttpGet(".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版的内容。



转载于:.html

更多推荐

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

本文发布于:2024-02-11 07:19:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1679813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主动   论坛   Android   HttpClient   discuz

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!