gpt4 book ai didi

android - 多个 Cookie header OKHttp JavaNetCookieJar

转载 作者:行者123 更新时间:2023-12-05 00:01:19 28 4
gpt4 key购买 nike

我一直在使用 JavaNetCookieJar OKHttp 库,代码在以下链接中。

https://github.com/square/okhttp/blob/master/okhttp-urlconnection/src/main/java/okhttp3/JavaNetCookieJar.java

JavaNetCookieJar 似乎有问题,它只将第一个 cookie 添加到请求中,而以前的响应已经收到多个“Set-Cookie”

在深入挖掘 git-hub 代码后,看起来我必须根据以下代码明确指定 cookie 列表“//RI 传递所有 header 。我们没有它们,所以我们没有通过他们!”

 @Override public List<Cookie> loadForRequest(HttpUrl url) {
// The RI passes all headers. We don't have 'em, so we don't pass 'em!
Map<String, List<String>> headers = Collections.emptyMap();
Map<String, List<String>> cookieHeaders;
try {
cookieHeaders = cookieHandler.get(url.uri(), headers);
} catch (IOException e) {
Platform.get().log(WARN, "Loading cookies failed for " + url.resolve("/..."), e);
return Collections.emptyList();
}

List<Cookie> cookies = null;
for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
String key = entry.getKey();
if (("Cookie".equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key))
&& !entry.getValue().isEmpty()) {
for (String header : entry.getValue()) {
if (cookies == null) cookies = new ArrayList<>();
cookies.addAll(decodeHeaderAsJavaNetCookies(url, header));
}
}
}

return cookies != null
? Collections.unmodifiableList(cookies)
: Collections.<Cookie>emptyList();
}

谁能帮我解决这个问题而不用硬编码我感兴趣的 cookie 列表,因为我需要所有 cookie。

感谢任何帮助。谢谢

最佳答案

我最终这样做了,但任何可靠的解决方案都值得赞赏

public class CookieStore implements CookieJar {

private final HashMap<String, List<Cookie>> _cookieStore = new HashMap<>();

@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {

List<Cookie> currentCookies = _cookieStore.get(url.host());
List<Cookie> responseCookies = new ArrayList(cookies);

if (currentCookies != null && responseCookies != null
&& responseCookies.size() > 0 && currentCookies.size() > 0) {
for (Cookie currentCookie : currentCookies) {

if (!responseCookies.contains(currentCookie) && (currentCookie.value()!=null && currentCookie.value()!="")) {
responseCookies.add(currentCookie);
}
}
}

_cookieStore.put(url.host(), responseCookies);
}

@Override
public List<Cookie> loadForRequest(HttpUrl url) {

List<Cookie> cookies = _cookieStore.get(url.host());
List<Cookie> requestCookies = new ArrayList<>();

if (cookies != null && cookies.size() > 0) {
for (Cookie currentCookie : cookies) {
if ((currentCookie.value()!=null && currentCookie.value()!="")) {
requestCookies.add(currentCookie);
}
}
}

return requestCookies != null ? requestCookies : new ArrayList<Cookie>();
}

关于android - 多个 Cookie header OKHttp JavaNetCookieJar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38582530/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com