问题描述
我试图让该支持Cookie一个URLConnection。根据该文件,我可以使用:
I'm trying to make a URLConnection that supports cookies. According to the documentation I can use:
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
我不能让这个code的工作,然后我看到这仅适用于API 9(2.3)。然而,我没有得到使用CookieManager错误在较旧的模拟器,CookieManager存在,但不能被构成。有没有什么办法,使这项工作的早期版本?我想:
I couldn't get this code to work, then I saw this only works for API 9 (2.3). However, I don't get an error using CookieManager in an older emulator, CookieManager exists, but can't be constructed. Is there any way to make this work for earlier versions? I tried:
cookieManager.setAcceptCookie(true);
URLConnection con = u.openConnection();
con.setRequestProperty("Cookie", cookieManager.getInstance().getCookie(url););
con.setDoOutput(true);
con.connect();
String addCookie = con.getHeaderField("Set-Cookie");
System.out.println(con.getHeaderFields().toString());
if (addCookie!=null) {
cookieManager.getInstance().setCookie(url, addCookie);
}
但这不起作用。
but this doesn't work.
推荐答案
我是能够使使用伊恩·布朗的CookieManager类饼干:http://www.hccp.org/java-net-cookie-how-to.html
I was able to enable cookies using Ian Brown's CookieManager class:http://www.hccp.org/java-net-cookie-how-to.html
我改名为IansCookieManager,设置类变量_CM =新IansCookieManager,现在它的简单:
I renamed it to IansCookieManager, set a class variable _CM = new IansCookieManager, now it's simple:
URLConnection conn = u.openConnection();
_CM.setCookies(conn);
conn.connect();
_CM.storeCookies(conn);
...
这篇关于的URLConnection的Cookies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!