使用 Java URLConnection 关闭 Cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1455856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cookies turned off with Java URLConnection
提问by dharga
I am trying to make a request to a webpage that requires cookies. I'm using HTTPUrlConnection, but the response always comes back saying
我正在尝试向需要 cookie 的网页发出请求。我正在使用 HTTPUrlConnection,但响应总是返回说
<div class="body"><p>Your browser's cookie functionality is turned off. Please turn it on.
How can I make the request such that the queried server thinks I have cookies turned on. My code goes something like this.
我如何发出请求,以便被查询的服务器认为我打开了 cookie。我的代码是这样的。
private String readPage(String page) throws MalformedURLException {
try {
URL url = new URL(page);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.connect();
InputStream in = uc.getInputStream();
int v;
while( (v = in.read()) != -1){
sb.append((char)v);
}
in.close();
uc.disconnect();
} catch (IOException e){
e.printStackTrace();
}
return sb.toString();
}
回答by ZZ Coder
You need to add a CookieHandler to the system for it handle cookie. Before Java 6, there is no CookieHandler implementation in the JRE, you have to write your own. If you are on Java 6, you can do this,
您需要向系统添加一个 CookieHandler 来处理 cookie。在 Java 6 之前,JRE 中没有 CookieHandler 实现,您必须自己编写。如果你使用的是 Java 6,你可以这样做,
CookieHandler.setDefault(new CookieManager());
URLConnection's cookie handling is really weak. It barely works. It doesn't handle all the cookie rules correctly. You should use Apache HttpClient if you are dealing with sensitive cookies like authentication.
URLConnection 的 cookie 处理真的很弱。它几乎不起作用。它不能正确处理所有 cookie 规则。如果您要处理身份验证等敏感 cookie,则应使用 Apache HttpClient。
回答by Serge Bogatyrev
I think server can't determine at the first request that a client does not support cookies. So, probably server sends redirects. Try to disable redirects:
我认为服务器无法在第一次请求时确定客户端不支持 cookie。因此,可能服务器发送重定向。尝试禁用重定向:
uc.setInstanceFollowRedirects(false);
Then you will be able to get cookies from response and use them (if you need) on the next request.
然后您将能够从响应中获取 cookie 并在下一个请求中使用它们(如果需要)。
回答by n00b
uc.getHeaderFields()
// get cookie (set-cookie) here
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
conn.addRequestProperty("Referer", "http://xxxx");
conn.addRequestProperty("Cookie", "...");
回答by Alex Marshall
If you're trying to scrape large volumes of data after a login, you may even be better off with a scripted web scraper like WebHarvest (http://web-harvest.sourceforge.net/) I've used it to great success in some of my own projects.
如果您在登录后尝试抓取大量数据,那么使用像 WebHarvest ( http://web-harvest.sourceforge.net/)这样的脚本化网络抓取工具可能会更好,我已经使用它取得了巨大的成功在我自己的一些项目中。

