我的代碼是這樣的:產生java.io.IOException:服務器返回的HTTP響應代碼:403網址
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
什麼是解決方案?
這個問題的一個很好的解釋會發現[這裏](http://stackoverflow.com/questions/31619746/why-should-i-access-a-url-using-a-user-agent) – manetsus