【发布时间】:2014-10-25 18:55:06
【问题描述】:
当我使用 cURL 时,在授权后,我会收到 Set-Cookie: JSESSIONID=xxx; Path=/; HttpOnly,但使用 Retrofit,尽管授权通过,我无法在 LogCat 中或通过 success 回调方法中的 response.getHeaders() 获得此信息。
我在这里做错了什么?
cURL 日志
wojciechko@Ubuntu ~> curl --data "login=wkr&password=wkr" 192.168.1.2:8080/login -v
* Hostname was NOT found in DNS cache
* Trying 192.168.1.2...
* Connected to 192.168.1.2 (192.168.1.2) port 8080 (#0)
> POST /login HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 192.168.1.2:8080
> Accept: */*
> Content-Length: 22
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 302 Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Set-Cookie: JSESSIONID=C6F875A1176AB9D373FB9D5BC7A6E5DF; Path=/; HttpOnly
< Location: http://192.168.1.2:8080/
< Content-Length: 0
< Date: Mon, 01 Sep 2014 13:33:02 GMT
<
* Connection #0 to host 192.168.1.2 left intact
改造日志
myclient D/Retrofit﹕ ---> HTTP POST http://192.168.1.2:8080/login
myclient D/Retrofit﹕ Content-Type: application/x-www-form-urlencoded; charset=UTF-8
myclient D/Retrofit﹕ Content-Length: 22
myclient D/Retrofit﹕ login=wkr&password=wkr
myclient D/Retrofit﹕ ---> END HTTP (22-byte body)
myclient D/Retrofit﹕ <--- HTTP 200 http://192.168.1.2:8080/login (187ms)
myclient D/Retrofit﹕ : HTTP/1.1 200 OK
myclient D/Retrofit﹕ Cache-Control: no-cache, no-store, max-age=0, must-revalidate
myclient D/Retrofit﹕ Content-Language: en-US
myclient D/Retrofit﹕ Content-Length: 3066
myclient D/Retrofit﹕ Content-Type: text/html;charset=utf-8
myclient D/Retrofit﹕ Date: Mon, 01 Sep 2014 14:00:20 GMT
myclient D/Retrofit﹕ Expires: 0
myclient D/Retrofit﹕ Last-Modified: Mon, 16 Jun 2014 14:12:14 GMT
myclient D/Retrofit﹕ OkHttp-Received-Millis: 1409580018720
myclient D/Retrofit﹕ OkHttp-Response-Source: NETWORK 200
myclient D/Retrofit﹕ OkHttp-Selected-Protocol: http/1.1
myclient D/Retrofit﹕ OkHttp-Sent-Millis: 1409580018711
myclient D/Retrofit﹕ Pragma: no-cache
myclient D/Retrofit﹕ Server: Apache-Coyote/1.1
myclient D/Retrofit﹕ X-Content-Type-Options: nosniff
myclient D/Retrofit﹕ X-Frame-Options: DENY
myclient D/Retrofit﹕ X-XSS-Protection: 1; mode=block
改造使用:
cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
final RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(MyClient.API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient())
.build();
MyClient.MyApi api = restAdapter.create(MyClient.MyApi.class);
api.login(login, password, new Callback<Response>() {
@Override
public void success(Response loginResponse, Response response) {
Toast.makeText(getApplicationContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "NOPE!", Toast.LENGTH_SHORT).show();
}
});
/******************/
public class MyClient {
public static final String API_URL = "http://192.168.1.2:8080";
interface MyApi {
@FormUrlEncoded
@POST("/login")
void login(@Field("login") String login, @Field("password") String password, Callback<Response> callback);
}
}
【问题讨论】:
-
您好,您的问题解决了吗?这很有趣,因为我在日志中也没有看到这些标题。
-
@grub- 我是这样做的:github.com/seeknresolve/SeekNResolveAndroid/blob/master/src/… 不确定这是否是正确的方法。
标签: android cookies retrofit okhttp