1

我们有一个任务,设计一个可以下载任何网页源代码的类。但是当我尝试测试我的代码并获取页面时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 = 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、头文件,但似乎没有任何效果。如果您对我有一些提示,我将不胜感激。

4

2 回答 2

5

编写一个 http 客户端,您必须考虑 gzip 编码以及分块传输。最好使用库来下载网页。

尝试这样的事情: http ://code.google.com/p/google-http-java-client/

于 2012-09-22T08:54:12.863 回答
2

您在问题中提到的网站似乎不尊重“接受”请求标头,也没有正确设置“内容编码”响应标头,我认为这是不正确的。

无论如何,您也可以使用java.util.zip.GZipInputStream以纯文本格式读取响应:

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);
}
于 2012-09-22T09:00:40.180 回答