I am trying to download JSON file from this URL: http://map01.eniro.no/search/search.json?index=yp&profile=pl&q=ma%C5%82opolskie&sortOrder=default&pageSize=5 in Java, but I always getting an exception:
我正在尝试从这个URL下载JSON文件:http://map01.eniro.no/search/search.json?在Java中,index=yp&p =pl&q=ma%C5%82opolskie&sortOrder= 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)
This is my function for this task. I tried a lot of different ways to solve this problem but I always get the same. I dont have much experience with HTTP connection programming so I need help.
这是我的功能。我尝试了很多不同的方法来解决这个问题,但是我总是得到同样的结果。我没有太多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());
}
UPDATE:
更新:
I check my http request by Fiddler app and I have an error in it:
我通过Fiddler app检查我的http请求,我有一个错误:
' Incorrectly formed Request-Line. abs_path was empty (e.g. missing /). RFC2616 '
的错误请求行形成的。abs_path是空的(例如:missing /)。RFC2616”
What does it mean? I thought that request is correct, because in browser I can read this json.
这是什么意思?我认为这个请求是正确的,因为在浏览器中我可以读取这个json。
2 个解决方案
#1
0
405 is Method Not Allowed
. The fact that you have parameters on a querystring suggests you should be doing a GET
not a POST
405是不允许使用的方法。查询字符串上有参数这一事实表明,您应该执行GET而不是POST操作
I would try changing con.setRequestMethod("POST");
to con.setRequestMethod("GET");
.
我尝试改变con.setRequestMethod(“POST”);con.setRequestMethod("得到");。
#2
0
as you wanted to get something from server you should make GET
request instead of POST
.
当你想从服务器上获取一些东西时,你应该发出get请求而不是POST。
Try this,
试试这个,
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;
}
#1
0
405 is Method Not Allowed
. The fact that you have parameters on a querystring suggests you should be doing a GET
not a POST
405是不允许使用的方法。查询字符串上有参数这一事实表明,您应该执行GET而不是POST操作
I would try changing con.setRequestMethod("POST");
to con.setRequestMethod("GET");
.
我尝试改变con.setRequestMethod(“POST”);con.setRequestMethod("得到");。
#2
0
as you wanted to get something from server you should make GET
request instead of POST
.
当你想从服务器上获取一些东西时,你应该发出get请求而不是POST。
Try this,
试试这个,
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;
}