12
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
    public static void main(String args[]) {
        try {
            processHTMLFromLink(new URL("http://fwallpapers.com"));
        } catch (MalformedURLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static int processHTMLFromLink(URL url) {
        InputStream is = null;
        DataInputStream dis;
        String line;
        int count = 0;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException mue) {
            System.out.println(mue.toString());
        } catch (IOException ioe) {
            System.out.println(ioe.toString());
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }
        return count;
    }
}

错误:

java.io.IOException: Server returned HTTP response code: 403 for URL: http://fwallpapers.com
Exception in thread "main" java.lang.NullPointerException
    at Test.processHTMLFromLink(Test.java:38)
    at Test.main(Test.java:15)
Java Result: 1

它在浏览器上运行良好。但我得到空点异常。此代码适用于其他链接。谁能帮我解决这个问题。出现 403 错误时如何获取内容。

4

3 回答 3

29

这是一篇旧帖子,但如果人们想知道它是如何工作的。

403 表示拒绝访问。有一个解决方法。如果你想做到这一点,你必须设置一个用户代理参数来“欺骗”网站

这就是我的旧方法的样子:

private InputStream read() {
try {
    return url.openStream();
 } 
catch (IOException e) {
  String error = e.toString();
  throw new RuntimeException(e);
 }
}

将其更改为:(它对我有用!)

private InputStream read() {
try {
    HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
    httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");

  return httpcon.getInputStream();
 } catch (IOException e) {
    String error = e.toString();
  throw new RuntimeException(e);
 }
}
于 2013-09-19T08:42:01.597 回答
0

现在这是一个完全不同的问题,所以我编辑了你的标题。

根据您的编辑,您没有收到空指针异常,而是收到 HTTP 403 状态,这意味着“禁止”,这意味着您无法访问该资源。

于 2012-07-04T04:11:25.850 回答
0

你的错误是吞下异常。

当我运行我的代码时,我得到一个 HTTP 403 - “禁止”。Web 服务器不允许您这样做。

我的代码非常适合http://www.yahoo.com

这是我的做法:

package url;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

/**
 * UrlReader
 * @author Michael
 * @since 3/20/11
 */
public class UrlReader {

    public static void main(String[] args) {
        UrlReader urlReader = new UrlReader();

        for (String url : args) {
            try {
                String contents = urlReader.readContents(url);
                System.out.printf("url: %s contents: %s\n", url, contents);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public String readContents(String address) throws IOException {
        StringBuilder contents = new StringBuilder(2048);
        BufferedReader br = null;

        try {
            URL url = new URL(address);
            br = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = "";
            while (line != null) {
                line = br.readLine();
                contents.append(line);
            }
        } finally {
            close(br);
        }

        return contents.toString();
    }

    private static void close(Reader br) {
        try {
            if (br != null) {
                br.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
于 2012-07-03T00:01:59.510 回答