18

改造リクエストにカスタム Cookie を設定する方法はありますか?

RequestInterceptorまたは他の手段を使用してですか?

4

4 に答える 4

2

これがretrofit2のやり方です

グレード:

compile 'com.squareup.retrofit2:retrofit:2.1.0'

コード:

static final class CookieInterceptor implements Interceptor {
            private volatile String cookie;

            public void setSessionCookie(String cookie) {
                this.cookie = cookie;
            }

            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                if (this.cookie != null) {
                    request = request.newBuilder()
                            .header("Cookie", this.cookie)
                            .build();
                }
                return chain.proceed(request);
            }
}


class Creator {

    public static MyApi newApi() {
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                .create();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new CookieInterceptor())
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyApi.URL)
                .callFactory(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        return retrofit.create(MyApi.class);
    }
}
于 2016-06-24T18:58:18.423 に答える
1

Another way to set a cookie is this way:

@Headers("Cookie: cookiename=cookievalue")
@GET("widget/list")
Call<List<Widget>> widgetList();

And here is a dynamic way:

@GET("user")
Call<User> getUser(@Header("Cookie") String cookie)
于 2018-05-03T20:55:40.843 に答える
0

私は RetroFit を使い始めたばかりですが、Cookie の処理方法が他のライブラリと同等ではないようです。私はこのようなことをしてしまいました:

// Set up system-wide CookieHandler to capture all cookies sent from server.
final CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);

// Set up interceptor to include cookie value in the header.
RequestInterceptor interceptor = new RequestInterceptor() {
  @Override
  public void intercept(RequestFacade request) {
    for (HttpCookie cookie : cookieManager.getCookieStore().getCookies()) {
      // Set up expiration in format desired by cookies
      // (arbitrarily one hour from now).
      Date expiration = new Date(System.currentTimeMillis() + 60 * 60 * 1000);
      String expires = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz")
          .format(expiration);

      String cookieValue = cookie.getName() + "=" + cookie.getValue() + "; " +
          "path=" + cookie.getPath() + "; " +
          "domain=" + cookie.getDomain() + ";" +
          "expires=" + expires;

      request.addHeader("Cookie", cookieValue);
    }
  }
};

RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint("https://api.github.com")
    .setRequestInterceptor(interceptor) // Set the interceptor
    .build();

GitHubService service = restAdapter.create(GitHubService.class);
于 2014-06-04T15:20:51.867 に答える