我的服务端在客户端响应上发送Set-Cookie,但在Android端我没有收到与标头中的Set-Cookie相关的任何内容。 我在Android上的网络堆栈是Retrofit 2.
这是我的请求代码,
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
String hostName = "https://www.mysupercoolhost.com";
CertificatePinner certificatePinner = new CertificatePinner.Builder()
.add(hostName, "sha256/p1")
.add(hostName, "sha256/p2")
.add(hostName, "sha256/p3")
.build();
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.certificatePinner(certificatePinner)
.build();
RequestBody rb = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), "{\"user\":\"test\"}");
Request request = new Request.Builder()
.method("POST", rb)
.addHeader("Content-Type", "application/json")
.url(hostName)
.addHeader("param1", "value1")
.addHeader("param2", "value2")
.build();
new Thread(new Runnable() {
@Override
public void run() {
try {
client.newCall(request).execute();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}).start();
这段相同的代码在Simple Java应用程序中运行,我可以看到在Set-Cookie键中转储的值,但在Android上失败。
请注意,我的java应用程序只是具有retrofit2依赖关系的maven项目。
在桌面上收到的Cookie类似于
INFO: Set-Cookie: test_443=9182912.910219021.0000; path=/
Mar 03, 2017 11:53:08 AM okhttp3.internal.Platform log
INFO: Set-Cookie: cutypie=wandarabigstring; Path=/
Mar 03, 2017 11:53:08 AM okhttp3.internal.Platform log
答案 0 :(得分:0)
最后,我得到它的工作,我不得不使用okhttpbuilder手动设置DNS。 我的代码在桌面上工作的原因是
cat /etc/hosts
有
127.0.0.1 localhost
255.255.255.255 broadcasthost
:1 localhost
x.x.x.x mysupercoolhost.com
我的Android设备没有这个信息
所以我不得不写
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.dns(new Dns() {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
return return Collections.unmodifiableList(Collections.singletonList(InetAddress.getByAddress("mysupercoolhost.com", domain_ip_in_bytes)));
}
});