2.3.4上的授权标头获得400个错误请求

时间:2014-05-09 20:46:35

标签: android android-2.3-gingerbread retrofit

我试图让我的简单应用在Android 2.3.4上工作 - 姜饼。我对服务器的第一个请求是用于身份验证。为了提出请求,我正在使用RetroFit。

但是,当我尝试从装有姜饼的手机发出请求时,我收到了以下错误。

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);

我正在构建适配器,如下所示:

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+)上运行得非常好。

为什么Authorization请求会在姜饼上失败?

1 个答案:

答案 0 :(得分:2)

我知道,旧线程,但我遇到了同样的问题,并没有在这里找到解决方案。所以我调查了一下,发现问题对我来说是什么。不过,我们使用Volley。

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

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

当我们干脆做的时候

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

它有效。

<强>为什么吗

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

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

相关问题