Android:HTTP POST响应 - 401

时间:2013-03-04 10:25:25

标签: android post

我遇到过一个问题 - 当您已登录并尝试访问另一个安全页面时,返回401错误消息。对我来说这个问题的神秘之处在于,如果通过Firefox RestCLient或iOS应用程序检查它,但无法通过Chrome Advanced Rest Client和Android应用程序访问,我可以在您登录后访问其他安全页面。但是,内容类型和其他必要的参数在Web工具和应用程序中设置相同。我曾尝试使用编码登录设置不同的auth标头:pass但它不需要t help and it doesn因为它应该没有它,我认为(至少FF和iOS应用程序没有这个标头工作)。怎么会错?

Chrome的响应标头:

401 Unauthorized

Loading time:
29
Request headers
Content-Type: application/x-www-form-urlencoded 
Response headers
Date: Mon, 04 Mar 2013 10:01:02 GMT 
Server: Apache/2.2.20 (Ubuntu) 
X-Powered-By: PHP/5.3.6-13ubuntu3.9
Set-Cookie: peachy=qg3mjvchjh1oionqlhhv0jrn71; path=/ 
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 
Vary: Accept-Encoding 
Content-Length: 96 
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8 

Firefox的响应标头:

Status Code: 200 OK
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Length: 202
Content-Type: application/json
Date: Mon, 04 Mar 2013 09:51:09 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=100
Pragma: no-cache
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.9

这是我在Android应用程序中的Restful代码的和平:

public String serverRequest(int action, Bundle params) {
    if (action == 0) {
        if (Const.DEBUG_ENABLED)
            Log.e(TAG, "You did not pass action.");
        return "You did not pass action.";
    }

    try {
        HttpRequestBase request = null;
        HttpPost postRequest = null;
        switch (action) {
            case SIGN_IN:
                request = new HttpPost();
                request.setURI(new URI(SIGNIN_URL));
                request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

                 postRequest = (HttpPost) request;

                 if (params != null) {
                 UrlEncodedFormEntity formEntity = new
                 UrlEncodedFormEntity(paramsToList(params));
                 postRequest.setEntity(formEntity);
                 }
                break;

            case SIGN_OUT:
                request = new HttpPost();
                request.setURI(new URI(SIGNOUT_URL));
                request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
                break;

            case BANK_CARD_VERIFY:
                request = new HttpPost();
                request.setURI(new URI(BANK_CARD_VERIFY_URL));
                request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

                postRequest = (HttpPost) request;

                if (params != null) {
                    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramsToList(params));
                    postRequest.setEntity(formEntity);
                }
                break;
        }

        if (request != null) {
            DefaultHttpClient client = new DefaultHttpClient();

            if (Const.DEBUG_ENABLED)
                Log.d(TAG, "Executing request: " + actionToString(action) + ": " + urlToString(action));

            HttpResponse response = client.execute(request);

            StatusLine responseStatus = response.getStatusLine();

            int statusCode = responseStatus != null ? responseStatus.getStatusCode() : 0;
            Log.d(TAG, "Status code: " + statusCode);
            }
            }

(登录和退出是公开的,bank_verify是受保护的页面.Android应用程序具有与Chrome相同的响应标头)。会话或别的东西似乎有问题,但我不确定。

修改 我好像发现了这里的问题。在Android应用程序中,我创建了一个新的HttpCLient对象,因为它丢失了所有旧数据。但另一个问题 - 如何使这个HttpCLient可重用?

1 个答案:

答案 0 :(得分:0)

发现问题。我每次都重复使用httpClient,每次只使用一个包含所有会话数据的httpClient。只需实现if语句来检查我是否已经有httpclient对象然后你创建一个新的。

相关问题