-1

您好,我的 okhttp3 拦截器有一个奇怪的错误。添加附加标头时,请求以超时结束。当相同的拦截器执行相同但不添加标头时,它可以正常工作。我的代码是这样的

    @Override
public Response intercept(Chain chain) throws IOException {
    if (timestamp == null) {
        timestamp = DateTime.now();
    }
    //this is used when user has forgotten his password and therefore we have no password and
    // don'T need a login to piranha
    if (StringUtils.isEmpty(password) && StringUtils.isEmpty(user)) {
        Response response = chain.proceed(chain.request());
        return response;
    } else {
        Request request = chain.request();
        RequestBody emptyBody;
        Request loginrequest;
        Response loginresponse_okhttp;
        Request return_request;
        emptyBody = RequestBody.create(null, new byte[0]);
        loginrequest = new Request.Builder()
                .url("https://my-fancy-backend/login")
                .method("POST", emptyBody)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("my-fancy-backend-user", user)
                .addHeader("my-fancy-backend-user", password)
                .build();
        try {

            if (timestamp != null && DateTime.now().isAfter(timestamp.plusHours(4))) {
                timestamp = DateTime.now();
                loginresponse_okhttp = client.newCall(loginrequest).execute();
                loginresponse = gson.fromJson(loginresponse_okhttp.body().charStream(), LoginResponse.class);
            }
            if (loginresponse == null) {
                timestamp = DateTime.now();
                loginresponse_okhttp = client.newCall(loginrequest).execute();
                loginresponse = gson.fromJson(loginresponse_okhttp.body().charStream(), LoginResponse.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return chain.proceed(request);
        }
        return_request = request.newBuilder().addHeader("o2online ", loginresponse.getTokenId()).build();
        return chain.proceed(return_request);

    }

}

我拦截的请求发送到另一台服务器,这有什么关系吗?或者这是向 okhttp3 请求添加额外标头的错误方法?

4

1 回答 1

0

如果有人来这里看看他的请求出了什么问题。我的问题被 chaning 解决了

 return_request = request.newBuilder().addHeader("o2online ", loginresponse.getTokenId()).build();

 return_request = request.newBuilder().addHeader("o2online", loginresponse.getTokenId()).build();

之后一切都很好。

于 2016-02-23T16:51:00.613 回答