我正在尝试从网站下载HTML文件。我使用以下简单方法:
URL url = new URL("here goes the link to the html file");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String htmlfile = "";
String temp;
while ((temp = br.readLine()) != null) {
htmlfile+= temp;
}
问题是我在htmlfile变量中得到以下字符串:
The installation of ... requires the acceptance of a cookie by your browser
software. The cookie is used to ensure that you and only you are
able to access information ....
换句话说,我需要在从网址打开流时启用cookie。是否可以通过使用URL实现此目的,还是需要不同的方法?提前致谢
答案 0 :(得分:3)
您可以使用addRequestProperty()
在URLConnection
对象上设置Cookie,例如
URL url = new URL("here goes the link to the html file");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Cookie", "here goes the cookie");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
如果事先不知道cookie,但是通过对先前HTTP请求的回复来设置,则可以在代表先前HTTP交换的URLConnection
实例上使用getHeaderFields()
等来检索cookie中。
答案 1 :(得分:1)