你好,我的okhttp3拦截器有一个奇怪的错误。添加addtional标头时,请求以超时结束。当相同的拦截做同样的但是没有添加标题时它工作正常。我的代码是这样的
@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请求添加其他标头的错误方法吗?
答案 0 :(得分:0)
如果有人来这里查看嘶嘶声请求出了什么问题。 我的问题是通过chaning来解决的。
return_request = request.newBuilder().addHeader("o2online ", loginresponse.getTokenId()).build();
到
return_request = request.newBuilder().addHeader("o2online", loginresponse.getTokenId()).build();
之后一切都很好。