我对这个错误有一个问题:
**Server returned HTTP response code: 501 for URL: http://dev1:8080/data/xml/01423_01.xml** 请参阅以下代码:
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;
}发布于 2009-09-10 19:00:01
501响应意味着“未实现”,并且通常被认为意味着服务器不理解您使用的HTTP方法(例如get、post等)。
我不认识ClientHttpRequest但是你有一句台词说
connection.setRequestMethod("GET");然后有一行字写着
input = postRequest.post();我不确定post()到底是做什么的,但这是否意味着发送一个POST请求?如果是这样,则这与第一行中指定的GET相矛盾。
无论采用哪种方法,服务器都会说它不支持GET或POST方法,无论您的代码实际发送的是哪种方法。您需要找出服务器支持该URL的方法,并使用该方法。
发布于 2009-09-10 18:05:17
也许你应该检查你的端口设置:
new URL(hostName + "/" + serviceName);看起来缺少端口号":8080“。
一些服务器期望客户端在请求中提供额外的信息,比如用户代理或一些表单数据。运行在服务器上的应用程序甚至可以期待cookie。您还应该检查完整的响应,而不仅仅是响应代码。
我建议您使用更方便的库,如:https://hc.apache.org/httpcomponents-client-ga/index.html
下面是一个简单的使用示例:
https://stackoverflow.com/questions/1406709
复制相似问题