如何使用Java下载受保护的网页
我们有一个任务,设计一个可以下载任何网页源代码的类。但当我尝试测试代码并获取页面时http://anidb.net/perl-bin/animedb.pl?show=main –什么都不起作用 这样的标准代码失败了:如何使用Java下载受保护的网页,java,security,download,automation,webpage,Java,Security,Download,Automation,Webpage,我们有一个任务,设计一个可以下载任何网页源代码的类。但当我尝试测试代码并获取页面时http://anidb.net/perl-bin/animedb.pl?show=main –什么都不起作用 这样的标准代码失败了: import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL link
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL link = new URL("http://www.anidb.net/");
BufferedReader in = new BufferedReader(
new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
以下是我得到的结果:
Šwq>²"¦§5´_ï__ÇUº=ôÙö?kŠ}~“bd`?l“Ïçz¢Çêõ>_"?j׉R“y}K¸\Ìc_DLÙªÏ_
–óMm_¼_0”•ö°ËC_aí½sî¤ìÁS ‚>dC0ìs_–y¹ñ±ÏÝÜAø%È_äÖá__æ©A@,4x„Š¶_ëɃ?
我尝试了所有东西:cookies、头文件,但似乎没有任何效果。如果你能给我一些提示,我将不胜感激 编写http客户端时,必须考虑gzip编码和分块传输。最好使用图书馆下载网页 试着这样做:
编写http客户机时,必须考虑gzip编码和分块传输。最好使用图书馆下载网页 试着这样做:
您在问题中提到的站点似乎不遵守“接受”请求标题,也没有正确设置“内容编码”响应标题,我认为这是不正确的 无论如何,您也可以使用java.util.zip.gzip输入流以纯文本格式读取响应:
public static void main(String[] args) throws Exception
{
URL link = new URL("http://www.anidb.net/");
HttpURLConnection con = (HttpURLConnection) link.openConnection();
GZIPInputStream in = new GZIPInputStream(con.getInputStream());
byte[] b = new byte[1024];
StringBuilder content = new StringBuilder();
while (in.read(b) > 0)
{
content.append(new String(b));
}
System.out.println(content);
}
您在问题中提到的站点似乎不支持“接受”请求头,也没有正确设置“内容编码”响应头,我认为这是不正确的 无论如何,您也可以使用java.util.zip.gzip输入流以纯文本格式读取响应:
public static void main(String[] args) throws Exception
{
URL link = new URL("http://www.anidb.net/");
HttpURLConnection con = (HttpURLConnection) link.openConnection();
GZIPInputStream in = new GZIPInputStream(con.getInputStream());
byte[] b = new byte[1024];
StringBuilder content = new StringBuilder();
while (in.read(b) > 0)
{
content.append(new String(b));
}
System.out.println(content);
}
这并不考虑字符编码。使用库。这并不考虑字符编码。使用一个库。这都是关于gzip的。我应该使用java.util.zip.gzip输入流。谢谢。这都是关于gzip的。我应该使用java.util.zip.gzip输入流。非常感谢。