tags:

views:

177

answers:

2

I am having an issue with this error:

**Server returned HTTP response code: 501 for URL: http://dev1:8080/data/xml/01423_01.xml**

See this code:

   private static Map sendRequest(String hostName, String serviceName) throws Exception {
         Map assets = null;
         HttpURLConnection connection = null;

         Authenticator.setDefault(new Authenticator());


         URL serviceURL = new URL(hostName + "/" + serviceName);
         connection = (HttpURLConnection)serviceURL.openConnection();
         connection.setRequestMethod("GET");
         ClientHttpRequest postRequest = new ClientHttpRequest(connection);

         InputStream input = null;


         /*

         At line input = postRequest.post(); I get the following error
         Server returned HTTP response code: 501 for URL: http://dev1:8080/data/xml/01423_01.xml

         Yet if I enter that url in my browser it opens up fine.  
         Is this a common problem? Is there some type of content type I need to set?
         */
         input = postRequest.post();
         connection.disconnect();
         return assets;
     }
+1  A: 

Perhaps you should check your port settings:

 new URL(hostName + "/" + serviceName);

Looks like the port number ":8080" is missing.

Some server expect additional information from the client in the request like a user agent or some form data. Even cookies could be expected by the application running on the server. You should also check the complete response and not only the response code.

I would recommend you to use a library like httpclient that is more convenient: http://hc.apache.org/httpcomponents-client/index.html

Here is simple usage example: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java

MrWhite
I used new URL("http://dev1:8080/data/xml/01423_01.xml"); and still didn't work. :(
joe
You need to specify the complete url like http://dev1:8080/data/xml/01423_01.xml
MrWhite
Somehow the h-t-t-p-:-/-/ is missing in the comments.
MrWhite
Sometimes a serves needs some more information in the request like a user agent or some form data.
MrWhite
+2  A: 

A 501 response means "not implemented", and is usually taken to mean that the server didn't understand the HTTP method that you used (e.g. get, post, etc).

I don't recognise ClientHttpRequest , but you have a line that says

connection.setRequestMethod("GET");

and then a line that says

input = postRequest.post();

I'm not sure what post() actually does, but does that mean send a POST request? If so, then that contradicts the GET specified in the first line.

Either way, the server is saying that it doesn't under the GET or the POST method, whichever one your code is actually sending. You need to find out what method the server does support for that URL, and use that.

skaffman
Very well explained! I am a bit late :( +1...
Aviator
ClientHttpRequest seems designed only to handle POST requests. You are supposed to use the getInputStream method of the URL class for GET requests.
Skip Head