无法使用Retrofit和OkHttp获取cookie

时间:2014-09-01 14:08:06

标签: android cookies retrofit okhttp

当我使用cURL时,在授权后,我会收到Set-Cookie: JSESSIONID=xxx; Path=/; HttpOnly,但是使用Retrofit,尽管授权通过,我仍然无法在LogCat或response.getHeaders() success中获得此权限回调方法。

我在这里做错了什么?

cURL log

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

1 个答案:

答案 0 :(得分:0)

您需要将记录器设置为其余适配器。

final RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint(MyClient.API_URL)
    .setLogLevel(RestAdapter.LogLevel.FULL)
    .setLog(new RestAdapter.Log() {
                @Override
                public void log(String s) {
                    Log.d("RestAdapter", s);
                }
            })
    .setClient(new OkClient())
    .build();