如何使用cookieManager在httpUrlConnection中处理cookie [英] How to handle cookies in httpUrlConnection using cookieManager

查看:960
本文介绍了如何使用cookieManager在httpUrlConnection中处理cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回多个Cookie的服务器请求,例如:





这是我如何存储这些cookie到cookieManager:

  HttpURLConnection connection = ...; 
static java.net.CookieManager msCookieManager = new java.net.CookieManager();
msCookieManager.put(COOKIES_URI,connection.getHeaderFields());

这是我将这些Cookie添加到下一个连接的方式:

  connection.setRequestProperty(Cookie,
msCookieManager.getCookieStore()。get(COOKIES_URI).toString());

这是从cookieManager获取cookie的正确方法吗?
相当肯定有更好的一个...

解决方案

好吧,正确的方法就是这样: p>

从响应标头获取Cookie并将其加载到cookieManager中

 code> static final String COOKIES_HEADER =Set-Cookie; 
HttpURLConnection connection = ...;
static java.net.CookieManager msCookieManager = new java.net.CookieManager();

Map< String,List< String>> headerFields = connection.getHeaderFields();
List< String> cookiesHeader = headerFields.get(COOKIES_HEADER);

if(cookiesHeader!= null){
for(String cookie:cookiesHeader){
msCookieManager.getCookieStore()。add(null,HttpCookie.parse (0));
}
}

从CookieManager获取Cookie,连接:

  if(msCookieManager.getCookieStore()。getCookies()。size()> 0){ 
//加入Cookies时,根据需要使用','或';'。大多数服务器使用';'
connection.setRequestProperty(Cookie,
TextUtils.join(;,msCookieManager.getCookieStore()。getCookies()));
}


I have a server request that returns multiple cookies, like that:

This is how I'm storing these cookies to the cookieManager:

HttpURLConnection connection = ... ;
static java.net.CookieManager msCookieManager = new java.net.CookieManager();
msCookieManager.put(COOKIES_URI, connection.getHeaderFields());

This is how I'm adding these cookies to the next connection:

connection.setRequestProperty("Cookie", 
  msCookieManager.getCookieStore().get(COOKIES_URI).toString());

Is it the right way to get the cookies from the cookieManager?, I'm quite sure there is a better one...

解决方案

Ok, the right way to do it is just like that:

Get Cookies from response header and load them into cookieManager:

static final String COOKIES_HEADER = "Set-Cookie";
HttpURLConnection connection = ... ;
static java.net.CookieManager msCookieManager = new java.net.CookieManager();

Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);

if (cookiesHeader != null) {
    for (String cookie : cookiesHeader) {
        msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
    }               
}

Get Cookies from cookieManager and load them into connection:

if (msCookieManager.getCookieStore().getCookies().size() > 0) {
    // While joining the Cookies, use ',' or ';' as needed. Most of the servers are using ';'
    connection.setRequestProperty("Cookie",
    TextUtils.join(";",  msCookieManager.getCookieStore().getCookies()));    
}

这篇关于如何使用cookieManager在httpUrlConnection中处理cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆