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:
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.
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:
' Incorrectly formed Request-Line. abs_path was empty (e.g. missing /). RFC2616 '
What does it mean? I thought that request is correct, because in browser I can read this json.
405 is
Method Not Allowed
. The fact that you have parameters on a querystring suggests you should be doing aGET
not aPOST
I would try changing
con.setRequestMethod("POST");
tocon.setRequestMethod("GET");
.