• Okhttp传递cookie给Webview的解决方法


    public class WebkitCookieProxy extends CookieManager implements CookieJar {
    
      private android.webkit.CookieManager mWebkitCookieManager;
    
      private static final String TAG = WebkitCookieProxy.class.getSimpleName();
    
      public WebkitCookieProxy() {
        this(null, null);
      }
    
      WebkitCookieProxy(CookieStore store, CookiePolicy cookiePolicy) {
        super(store, cookiePolicy);
        this.mWebkitCookieManager = android.webkit.CookieManager.getInstance();
      }
    
      @Override
      public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException{
        // make sure our args are valid
        if ((uri == null) || (responseHeaders == null)) {
          return;
        }
    
        // save our url once
        String url = uri.toString();
    
        // go over the headers
        for (String headerKey : responseHeaders.keySet()) {
          // ignore headers which aren't cookie related
          if ((headerKey == null)
              || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey
              .equalsIgnoreCase("Set-Cookie"))) {
            continue;
          }
    
          // process each of the headers
          for (String headerValue : responseHeaders.get(headerKey)) {
            mWebkitCookieManager.setCookie(url, headerValue);
          }
        }
      }
    
      @Override
      public Map<String, List<String>> get(URI uri,
          Map<String, List<String>> requestHeaders) throws IOException {
        // make sure our args are valid
        if ((uri == null) || (requestHeaders == null)) {
          throw new IllegalArgumentException("Argument is null");
        }
    
        // save our url once
        String url = uri.toString();
    
        // prepare our response
        Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
    
        // get the cookie
        String cookie = mWebkitCookieManager.getCookie(url);
    
        // return it
        if (cookie != null) {
          res.put("Cookie", Arrays.asList(cookie));
        }
    
        return res;
      }
    
      @Override
      public CookieStore getCookieStore() {
        // we don't want anyone to work with this cookie store directly
        throw new UnsupportedOperationException();
      }
    
      @Override
      public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        HashMap<String, List<String>> generatedResponseHeaders = new HashMap<>();
        ArrayList<String> cookiesList = new ArrayList<>();
        for (Cookie c : cookies) {
          // toString correctly generates a normal cookie string
          cookiesList.add(c.toString());
        }
    
        generatedResponseHeaders.put("Set-Cookie", cookiesList);
        try {
          put(url.uri(), generatedResponseHeaders);
        } catch (IOException e) {
          Log.e(TAG, "Error adding cookies through okhttp", e);
        }
      }
    
      @Override
      public List<Cookie> loadForRequest(HttpUrl url) {
        ArrayList<Cookie> cookieArrayList = new ArrayList<>();
        try {
          Map<String, List<String>> cookieList = get(url.uri(), new HashMap<String, List<String>>());
          // Format here looks like: "Cookie":["cookie1=val1;cookie2=val2;"]
          for (List<String> ls : cookieList.values()) {
            for (String s : ls) {
              String[] cookies = s.split(";");
              for (String cookie : cookies) {
                Cookie c = Cookie.parse(url, cookie);
                cookieArrayList.add(c);
              }
            }
          }
        } catch (IOException e) {
          Log.e(TAG, "error making cookie!", e);
        }
        return cookieArrayList;
      }
    }

    public static OkHttpClient createHttpClient() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder().cookieJar(new WebkitCookieProxy());
    return builder.build();
    }
     
  • 相关阅读:
    BZOJ 1996: [Hnoi2010]chorus 合唱队
    BZOJ 2431: [HAOI2009]逆序对数列
    BZOJ1013: [JSOI2008]球形空间产生器sphere
    BZOJ 4196: [Noi2015]软件包管理器
    BZOJ 3670: [Noi2014]动物园
    NOIP 2017 提高组 day1t2 时间复杂度
    loj #6278. 数列分块入门 2
    CF285 E Positions in Permutations——“恰好->大于”的容斥和允许“随意放”的dp
    洛谷 1969 积木大赛——水题
    洛谷 1965 转圈游戏——水题
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/11040832.html
Copyright © 2020-2023  润新知