我的代码是这样的:
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
解决办法是什么?