发送多个获取请求
问题描述:
我正在编写代码以发送获取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)
答
我觉得这个,如果你使用的OAuth2验证到谷歌才会工作。我试图从我的浏览器访问网址,并得到相同的404错误消息。
您可能试图访问限于您的用户帐户的供稿。
使用Youtube API Client for Java并检查samples以了解如何验证并向Youtube API发出请求。
当您的代码发出GET请求时,您会得到什么HTTP状态码? – Bobulous 2014-09-03 20:03:39
如果不是200秒,返回什么响应代码?发布程序的调试输出。 – 2014-09-03 20:04:29
您是否尝试过在浏览器中点击该网址?猜猜返回的代码是什么? – 2014-09-04 03:33:09