Android:HTTP POST响应-401

Android:HTTP POST响应-401,android,post,Android,Post,我遇到了一个问题-当您已经登录并试图访问另一个安全页面时返回401错误消息。对我来说,这个问题的神秘之处在于,如果通过Firefox RestCLient或iOS应用程序进行检查,我可以在您登录后访问另一个安全页面,但无法通过Chrome Advanced Rest Client和Android应用程序进行访问。但是,web工具和应用程序中的内容类型和其他必要参数设置相同。我曾尝试使用编码的login:pass设置不同的身份验证标题,但它没有帮助,也不需要,因为我认为没有它它应该可以工作(至少F

我遇到了一个问题-当您已经登录并试图访问另一个安全页面时返回401错误消息。对我来说,这个问题的神秘之处在于,如果通过Firefox RestCLient或iOS应用程序进行检查,我可以在您登录后访问另一个安全页面,但无法通过Chrome Advanced Rest Client和Android应用程序进行访问。但是,web工具和应用程序中的内容类型和其他必要参数设置相同。我曾尝试使用编码的login:pass设置不同的身份验证标题,但它没有帮助,也不需要,因为我认为没有它它应该可以工作(至少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);
            }
            }
(登录和注销是公开的,银行验证是安全页面。Android应用程序的响应标题与chrome相同)。看起来会话或其他方面有问题,但我不太确定

编辑:
看来我发现了这里的问题所在。在Android应用程序中,我创建了一个新的HttpCLient对象,因为所有旧数据都丢失了。但还有一个问题-如何使此HttpCLient可重用?

发现了一个问题。我每次都重用httpClient,尽管每次只使用一个拥有所有会话数据的客户端。只要实现if语句来检查我是否已经有了httpclient对象,然后您就可以创建一个新的对象。

如果您找到了答案,请将其作为答案发布并接受它。对于新问题,请打开新问题。