简体   繁体   English

Android:HTTP POST响应-401

[英]Android: HTTP POST response - 401

I have faced with one problem - returning 401 error msg when you already signed in and trying to get access to another secured pages. 我遇到了一个问题-当您已经登录并尝试访问另一个受保护的页面时,返回401错误消息。 The mystic of this problem for me is that I can get access to another secured pages after you are signed in if check it via Firefox RestCLient or via iOS app but cannot get access via Chrome Advanced Rest Client and Android app. 对我来说,这个问题的神秘之处在于,如果您通过Firefox RestCLient或iOS应用程序进行检查,则可以在登录后访问另一个安全页面,但无法通过Chrome Advanced Rest Client和Android应用程序进行访问。 However, content-type and other necessary params are set the same in both web tools and apps. 但是,内容类型和其他必要的参数在Web工具和应用程序中均设置为相同。 I have tried to set different auth headers with encoded login:pass but it doesn t help and it doesn t need because it should work without it, I think(at least FF and iOS app work without this header). 我试图用编码的login:pass设置不同的auth标头,但是它没有t help and it doesn需要,因为我认为,如果没有它,它应该可以工作(至少FF和iOS app没有此标头就可以工作)。 What`s gonna be wrong? 怎么了?

Response headers of Chrome: 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 

Response headers of Firefox: 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

That is my peace of Restful code in Android app: 这就是我在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);
            }
            }

(sign in and sign out are public, bank_verify is secured page. Android app has the same response headers like chrome). (登录和登出都是公开的,bank_verify是安全页面。Android应用具有与chrome相同的响应标头)。 It seems there is something problem with session or something else but I`m not sure exactly. 会话或其他方面似乎有问题,但我不确定。

EDIT: It seems I have found what`s the problem here. 编辑:看来我在这里找到了什么问题。 In Android app I create a new HttpCLient object due to it all old data is losed. 在Android应用程序中,由于所有旧数据丢失,我创建了一个新的HttpCLient对象。 But another question - how to make this HttpCLient reusable? 但是另一个问题-如何使此HttpCLient可重用?

Problem found. 发现问题。 I reuse httpClient everytime in spite of that to use only one which has all session data everytime. 我每次都重用httpClient,尽管每次仅使用一个具有所有会话数据的httpClient。 Just implement if statement to check if I have already httpclient object then you it otherwise create a new. 只需执行if语句来检查我是否已经有了httpclient对象,那么您便可以创建一个新的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM