我正在尝试从此URL下载JSON文件:Java中的http://map01.eniro.no/search/search.json?index=yp&profile=pl&q=ma%C5%82opolskie&sortOrder=default&pageSize=5,但我总是遇到异常:
HTTP Status 500 - Server returned HTTP response code: 405 for URL: http://map01.eniro.no/search/search.json
type Exception report
message Server returned HTTP response code: 405 for URL: http://map01.eniro.no/search/search.json
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.io.IOException: Server returned HTTP response code: 405 for URL: http://map01.eniro.no/search/search.json
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
MainServlet.callURL(MainServlet.java:108)
MainServlet.doGet(MainServlet.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.io.IOException: Server returned HTTP response code: 405 for URL: http://map01.eniro.no/search/search.json
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
java.net.HttpURLConnection.getResponseCode(Unknown Source)
MainServlet.callURL(MainServlet.java:102)
MainServlet.doGet(MainServlet.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
这是我执行此任务的功能。我尝试了很多不同的方法来解决这个问题,但我总是这样。我对HTTP连接编程没有太多经验,所以我需要帮助。
public static void callURL() throws IOException {
URL obj = new URL("http://map01.eniro.no/search/search.json");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36");
con.setRequestProperty("Accept-Language", "pl-PL;q=0.8");
String urlParameters = "?index=yp&profile=pl&q=małopolskie&sortOrder=default&pageSize=5";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + myURL);
System.out.println("Post parameters : " + urlParameters);
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();
System.out.println(response.toString());
}
更新
我通过Fiddler应用程序检查我的http请求,但我有一个错误:
'请求行格式错误。 abs_path为空(例如缺少/)。 RFC2616'
这是什么意思?我认为这个请求是正确的,因为在浏览器中我可以阅读这个json。
答案 0 :(得分:0)
405是Method Not Allowed
。你在查询字符串上有参数的事实表明你应该做GET
而不是POST
我会尝试改变
con.setRequestMethod("POST");
至con.setRequestMethod("GET");
。
答案 1 :(得分:0)
由于您希望从服务器获取内容,因此您应该GET
请求而不是POST
。
试试这个,
private static JSONObject getResult() throws JSONException {
String uri = "http://map01.eniro.no/search/search.json?index=yp&profile=pl&q=ma%C5%82opolskie&sortOrder=default&pageSize=5";
JSONObject jsonResponse = null;
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String jsonResponseString = br.readLine();
jsonResponse = new JSONObject(jsonResponseString);
conn.disconnect();
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (JSONException ex) {
}
System.out.println("res: " + jsonResponse);
return jsonResponse;
}