0
我正在編寫代碼以發送獲取URL列表的請求。現在,一些獲取請求失敗並且不返回HTTP響應代碼200.但是,當我在瀏覽器上獲取請求的URL時,我可以下載XML文件(這是應該發生的事情)。我的第一個問題是爲什麼會發生這種情況,當我從我的代碼發送請求時,請求會失敗,但不會失敗。發送多個獲取請求
我的第二個問題是我該如何解決失敗的get請求的這個問題。
請在下面找到我在代碼中編寫的代碼示例。
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
唯一的例外是:
Update:
Response Code : 404
java.io.FileNotFoundException: http://gdata.youtube.com/feeds/api/videos/gzakooXyvuA
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at ReadMyExcel.sendGet(ReadMyExcel.java:133)
at ReadMyExcel.readExcel(ReadMyExcel.java:82)
at ReadMyExcel.main(ReadMyExcel.java:99)
當您的代碼發出GET請求時,您會得到什麼HTTP狀態碼? – Bobulous 2014-09-03 20:03:39
如果不是200秒,返回什麼響應代碼?發佈程序的調試輸出。 – 2014-09-03 20:04:29
您是否嘗試過在瀏覽器中點擊該網址?猜猜返回的代碼是什麼? – 2014-09-04 03:33:09