相关文章推荐
千年单身的围巾  ·  reactjs - How does ...·  1 年前    · 
玩手机的跑步鞋  ·  【Share ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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;

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.

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 Sep 10, 2009 at 20:25

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: https://hc.apache.org/httpcomponents-client-ga/index.html

Here is simple usage example:
https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientWithResponseHandler.java

Sometimes a serves needs some more information in the request like a user agent or some form data. – MrWhite Sep 10, 2009 at 18:47

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.