如何使用的CookieStore获得在HttpURLConnection的饼干?(How to ge

2019-07-29 15:52发布

在我的Application类中,我做到以下几点:

public class MyApplication extends Application {
    private static HttpURLConnection conn = null;
    public static CookieStore cookieStore;
    public static HttpContext localContext;
    public static DefaultHttpClient client = new DefaultHttpClient();

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        CookieSyncManager.createInstance(this);
        cookieStore = new BasicCookieStore();
        localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    }
    ...
}

而且我有一个连接Runnable部分:

HttpURLConnection conn = null;
URL url;
try {
    url = new URL(requestUrl);
    conn = (HttpURLConnection) url.openConnection();                
    if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
        System.setProperty("http.keepAlive", "false");
    }
    conn.setConnectTimeout(8000);
    conn.setRequestMethod(method);
    conn.setInstanceFollowRedirects(false);
    conn.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");

    MyApplication app = (MyApplication) mContext.getApplicationContext();

*******************************************     
    if(app.cookieStore.getCookies()!=null){
        conn.setRequestProperty("Cookie", app.cookieStore.getCookies().toString());
        Log.d("tag", "cookie get " +  cookie.getCookie(mContext.getString(R.string.host_url))); 
    }   
********** this part not work **********    


    Map m = conn.getHeaderFields();
    if (m.containsKey("Set-Cookie")) {
        String cookies = "";
        Collection c = (Collection) m.get("Set-Cookie");
        for (Iterator i = c.iterator(); i.hasNext();) {
            cookies += (String) i.next() + ",";
        }
        cookie.setCookie(mContext.getString(R.string.host_url), cookies);
        Log.d("tag", "cookie set " +  cookies);
    }   
} catch(...) { ... }

我得到Cookiee使用cookieStore并设置cookies,但我什么也没有,则返回null
如何使用CookieStore

一些示例和答案给这个:

List<Cookie> cookies =  app.client.getCookieStore().getCookies();

它也返回null我。

Answer 1:

使用此设置cookie。

首先,设置upcookieManager:

    cookieManager = new java.net.CookieManager();
    CookieHandler.setDefault(cookieManager);
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

接下来,通过调用setRequestProperty设置cookies来HttpURLConnection的()

        if (cookieManager.getCookieStore().getCookies().size() > 0) {

            List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();

            if (cookies != null) {
                for (HttpCookie cookie : cookies) {
                    conn.setRequestProperty("Cookie", cookie.getName() + "=" + cookie.getValue());
                }
            }
        }


Answer 2:

设置cookie的:

conn.setRequestProperty("Cookie", "cookieName=cookieValue; domain=www.test.com");

得到的cookie:

Map<String, List<String>> headerFields = conn.getHeaderFields();
List<String> cookiesHeader = headerFields.get("Set-Cookie");
if(cookiesHeader != null){
    String cookie = cookiesHeader.get(0);
    HttpCookie httpCookie = HttpCookie.parse(cookie).get(0);
    String name = httpCookie.getName(); 
    String value = httpCookie.getValue();
    String domain = httpCookie.getDomain();
}


文章来源: How to get cookies in HttpUrlConnection by using CookieStore?