2.3.4 上的授权标头获得 400 错误请求 [英] Authorization header on 2.3.4 gets 400 bad request

查看:49
本文介绍了2.3.4 上的授权标头获得 400 错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的简单应用在 Android 2.3.4 上运行 - 姜饼.我向服务器发出的第一个请求是进行身份验证.为了提出请求,我正在使用 RetroFit.

I'm trying to get my simple app working on Android 2.3.4 - gingerbread. The first request I make to the server is for authentication. For making requests I am using RetroFit.

但是,当我尝试从带有姜饼的手机发出请求时遇到以下错误.

However, I'm getting the error below when trying to make a request from a phone that has gingerbread on it.

05-09 16:21:44.724    4706-4734/com.myapp.mobile D/Retrofit﹕ <--- HTTP 400 https://myserver.com/myservice/user/signin (2318ms)
05-09 16:21:44.740    4706-4734/com.myapp.mobile D/Retrofit﹕ <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>400 Bad Request</title>
    </head><body>
    <h1>Bad Request</h1>
    <p>Your browser sent a request that this server could not understand.<br />
    </p>
    <hr>
    <address>Apache/2.2.15 (CentOS) Server at localhost Port 443</address>
    </body></html>

我提出的要求是:

@GET("/user/signin")
@Headers({"Content-type: application/json"})
User signin(@Header("Authorization") String value);

我正在像这样构建适配器:

I'm building the adapter like this:

private final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
        .setServer(API_URL)
        .setLogLevel(RestAdapter.LogLevel.FULL)
        .build();

private final MyTaskService MY_SERVICE = REST_ADAPTER.create(MyTaskService.class);

相同的代码库在较新的 API 上运行良好(我已经尝试过 API 15+).

The same code base works perfectly fine on a newer API (I've tried API 15+).

Authorization 请求在姜饼上失败有什么原因吗?

Is there any reason why the Authorization request would fail on gingerbread?

推荐答案

我知道,旧线程,但我遇到了同样的问题并且没有在这里找到解决方案.所以我调查并发现了我的问题.不过,我们使用 Volley.

I know, old thread, but I ran into the same problem and didn't find a solution here. So I investigated and found what the problem was for me. We use Volley, though.

实际问题是在使用如下代码时:

The actual problem was when using code like:

    String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
    headers.put("Authorization", auth);

当我们简单地做

    headers.put("Authorization", "Basic asd123ASD123asd");

它有效.

为什么?

好吧,我不知道为什么Android 2.3 的行为当然与较新的 Android 不同,但是 Base64.encodeToString() 引入了一个换行符.我认为这是我们使用 Wireshark.这条新行将 HTTP 标头分成两部分,因此您会从服务器获得两个响应,后一个响应是 400 Bad Request,显示在 Logcat 中.

Well, I don't know why Android 2.3 behaves differently than the newer Androids, of course, but that Base64.encodeToString() introduced a new line character. I figured this our using Wireshark. This new line breaks the HTTP header into two, hence you get two responses from the server with the latter one being a 400 Bad Request, which is displayed in Logcat.

如果有人对我是如何想到的感兴趣,请不要犹豫问!但是由于这个问题是引用旧的并且没有回应,我想没有太多兴趣并且我节省了解释HTTP和截图的时间.

If someone is interested in how I figured this out, don't hestitate to ask! But since this question is quote old and without response, I guess there is not much interest and I save the time of explaining HTTP and taking screenshots.

这篇关于2.3.4 上的授权标头获得 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆