在Retrofit中设置自定义cookie

 拍友2502893767 发布于 2023-01-29 17:05

有没有办法在改装请求上设置自定义cookie?

通过使用RequestInterceptor或任何其他方式?

1 个回答
  • 通过retrofit.RequestInterceptor:

    @Override
    public void intercept(RequestFacade request) {    
         request.addHeader("Cookie", "cookiename=cookievalue");
    }
    

    您可以RequestInterceptor按如下方式设置自定义:

    String cookieKey = ...
    String cookieValue = ...
    
    RestAdapter adapter = new RestAdapter.Builder()
        .setRequestInterceptor(new RequestInterceptor() {
          @Override
          public void intercept(RequestFacade request) {
            // assuming `cookieKey` and `cookieValue` are not null 
            request.addHeader("Cookie", cookieKey + "=" + cookieValue);
          }
        })
        .setServer("http://...")
        .build();
    
    YourService service = adapter.create(YourService.class);
    

    要读取服务器设置的任何cookie,请附加如下自定义cookie管理器:

    OkHttpClient client = new OkHttpClient();
    CustomCookieManager manager = new CustomCookieManager();
    client.setCookieHandler(manager);
    
    RestAdapter adapter = new RestAdapter.Builder()
        .setClient(new OkClient(client))
        ...
        .build();
    

    哪里CustomCookieManager可能是这样的:

    public class CustomCookieManager extends CookieManager {
    
      // The cookie key we're interested in.    
      private final String SESSION_KEY = "session-key";
    
      /**
       * Creates a new instance of this cookie manager accepting all cookies.
       */
      public CustomCookieManager() {
        super.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
      }
    
      @Override
      public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
    
        super.put(uri, responseHeaders);
    
        if (responseHeaders == null || responseHeaders.get(Constants.SET_COOKIE_KEY) == null) {
          // No cookies in this response, simply return from this method.
          return;
        }
    
        // Yes, we've found cookies, inspect them for the key we're looking for.
        for (String possibleSessionCookieValues : responseHeaders.get(Constants.SET_COOKIE_KEY)) {
    
          if (possibleSessionCookieValues != null) {
    
            for (String possibleSessionCookie : possibleSessionCookieValues.split(";")) {
    
              if (possibleSessionCookie.startsWith(SESSION_KEY) && possibleSessionCookie.contains("=")) {
    
                // We can safely get the index 1 of the array: we know it contains
                // a '=' meaning it has at least 2 values after splitting.
                String session = possibleSessionCookie.split("=")[1];
    
                // store `session` somewhere
    
                return;
              }
            }
          }
        }
      }
    }
    

    2023-01-29 17:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有