<span style="font-size:18px;">//设置网络请求cookie CookieSyncManager.createInstance(this);</span>
<span style="font-size:18px;"> protected String getCookie() { CookieManager cookieManager = CookieManager.getInstance(); String cookie = cookieManager.getCookie("cookie"); if (cookie != null) { return cookie; } else { return ""; } } protected void setCookie(String cookie) { CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setCookie("cookie", cookie); }</span>
uc = (HttpURLConnection) new URL(url + "?" + paramsStr.toString()).openConnection(); uc.setRequestProperty("Cookie", getCookie());
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'宋体';font-size:12.0pt;"><span style="background-color:#344134;">setCookies</span>(uc.getHeaderFields())<span style="color:#cc7832;">;</span>
3. 过期按需处理一下
附1:cookie的获取
response.headerFields = conn.getHeaderFields(); setCookies(response.headerFields);
/** * 存储cookie * @param headerFields */ protected void setCookies(Map<String, List<String>> headerFields) { if (null == headerFields) { return; } List<String> cookies = headerFields.get(COOKIES_KEY); if (null == cookies) { return; } for (String cookie : cookies) { setCookie(cookie); } }