Java 服务器为url openStream()返回403
错误:Java 服务器为url openStream()返回403,java,html,Java,Html,错误: 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 { processHTMLF
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;
}
}
它在浏览器上运行良好。但我得到了零点异常。这段代码可以很好地处理其他链接。有人能帮我解决这个问题吗。当我收到403错误时,如何获取内容。您的错误正在吞噬异常 当我运行代码时,我得到一个HTTP403-“禁止”。web服务器不允许您这样做 我的代码非常适合你 我是这样做的:
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
这是一个完全不同的问题,所以我编辑了你的标题
根据您的编辑,您没有得到空指针异常,您得到的是HTTP 403状态,这意味着“禁止”,这意味着您无法访问该资源。这是一篇老文章,但如果人们想知道这是如何工作的 403表示访问被拒绝。 这方面有一个解决办法。 如果你想做到这一点,你必须设置一个用户agant参数来“愚弄”网站 我的旧方法是这样的:
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();
}
}
}
将其更改为:(而且它对我有效!)
但是您在这里打开了两个连接,这是浪费:一个用于
openConnection()
,另一个用于openStream()。
使用openConnection()
,存储生成的URLConnection
对象,并从连接对象获取内容类型和流。因此不显示行号,并且您的代码不是SSCCE。发布SSCCE将为您带来更多帮助。至少,编辑您的帖子并指出所显示代码中的哪一行对应于trace.System.out.println(url.openConnection().getContentType())中的“82”;,我用它检查连接类型。这是暂时的。第82行是{is.close();},因为“is”为空。代码片段与其他链接配合良好。
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);
}
}