0

所以我试图从这个服务器成功登录:www.liuserver.com 和我使用两个类 LoginLayout.java 和 CustomHttpclient.java,现在我已经使用 Httpwatch 监视浏览器与服务器的通信以跟踪类型回复服务器生成的帖子(生成包含用户提供的用户名和密码的 url 以及自动生成的 x 和 y 整数,在此示例中为 31 和 1,其他时候嘿只是零)发布到网页看起来像这样 :

POST /login.php HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap,     application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: https://www.liuserver.com/login.php
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR     3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: www.liuserver.com
Content-Length: 38
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: PHPSESSID=e3q4d78jkci3shhn8fq1tlf7i5

USER=20730312&PASS=Password44&x=32&y=1

现在我已成功生成带有参数的 url,我将它们发布到服务器,但当然它给了我否定的答复,现在我被告知,在 httpwatch 插件的 cookie 选项卡中很明显我需要发送phpsessionid 作为 cookie,我的问题是如何做到这一点,我应该在哪个类中放置用于发送带有参数的 cookie 的代码,以及如何在成功验证后跟踪服务器的回复类型的用户名和密码。回复流看起来像:

HTTP/1.1 302 Found
Server: nginx/1.0.14
Date: Tue, 01 May 2012 15:57:06 GMT
Content-Type: text/html
Connection: keep-alive
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: student/
X-Powered-By: PleskLin
Content-Length: 0

登录页面的完整网址:https://www.liuserver.com/login.php

更新 :

好的,我已成功从服务器获取 phpsessionid cookie,我可以查看它的值,现在我必须使用将我的登录参数提交到 erver

response = CustomHttpClient.executeHttpPost("https://www.liuserver.com/login.php", postParameters);

但是我应该在哪里放置包含 phpsession id 的 cookie 在这个方法中执行 httpPost 我可以更改方法的参数并将 cookie 传递到那里吗?

4

2 回答 2

1

要保存 cookie,您需要以下内容:

CookieStore cookieStore = new BasicCookieStore();
DefaultHttpClient httpclient = new DefaultHttpClient();

HttpContext ctx = new BasicHttpContext();
ctx.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet get = new HttpGet("your URL here");

HttpResponse response = httpclient.execute(get,ctx);

如果你想在请求之间保留 cookie,你必须为每个请求重用cookieStorectx

此外,您可以阅读您的cookieStore内容以查看里面的内容:

List<Cookie> cookies = cookieStore.getCookies();
if( !cookies.isEmpty() ){
    for (Cookie cookie : cookies){
        String cookieString = cookie.getName() + " : " + cookie.getValue();
        Log.info(TAG, cookieString);
    }
}
于 2012-05-01T17:00:54.133 回答
0

Your cookie is passed as part of the headers in the HTTP reply (just like in your POST) if you want to play with the headers directly.

But like the other poster says, how to deal with cookies (properly) is in the example he has posted, and it will ultimately help you to use the API properly, even though it may be a pain to learn how it works the first time around :-)

于 2012-05-03T08:09:08.240 回答