java.io.IOException: 服务器返回 HTTP 响应代码: 403 for URL
2022-09-01 15:17:19
我的代码是这样的:
URL url;
URLConnection uc;
StringBuilder parsedContentFromUrl = new StringBuilder();
String urlString="http://www.example.com/content/w2e4dhy3kxya1v0d/";
System.out.println("Getting content for URl : " + urlString);
url = new URL(urlString);
uc = url.openConnection();
uc.connect();
uc.getInputStream();
BufferedInputStream in = new BufferedInputStream(uc.getInputStream());
int ch;
while ((ch = in.read()) != -1) {
parsedContentFromUrl.append((char) ch);
}
System.out.println(parsedContentFromUrl);
但是,当我尝试通过浏览器访问URL时,没有问题,但是当我尝试通过java程序访问它时,它会引发期望:
java.io.IOException: Server returned HTTP response code: 403 for URL
解决方案是什么?