2009-09-10 385 views
0

我有這個錯誤的問題:的Java HTTP調用返回響應代碼:501

**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; 
    } 
+0

什麼是ClientHttpRequest?它是一個Sun Java API嗎? – vpram86 2009-09-10 18:55:24

回答

3

A 501迴應表示「沒有實現」,並且通常取意味着服務器不理解你使用的HTTP方法(例如get,post等)。

我不承認ClientHttpRequest,但你有這樣一行

connection.setRequestMethod("GET"); 

,然後這樣一行

input = postRequest.post(); 

我不知道是什麼職位()實際上做,但這是否意味着發送POST請求?如果是這樣,那麼與第一行中指定的GET相矛盾。

無論哪種方式,服務器都說它不在GET或POST方法下,無論你的代碼實際發送的是哪一個。你需要找出服務器支持該URL的方法,並使用它。

+0

很好解釋!我有點遲到:(+1 ... – vpram86 2009-09-10 19:00:57

+0

ClientHttpRequest似乎只設計用於處理POST請求,你應該使用URL的getInputStream方法類的GET請求。 – 2009-09-10 20:25:17

1

也許你應該檢查你的端口設置:

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

看起來像缺少端口號「:8080」。

某些服務器期望來自客戶端的附加信息,如用戶代理或某些表單數據。即使在服務器上運行的應用程序也可以預期cookie。您還應該檢查完整的回覆,而不僅僅是回覆代碼。

我會建議你使用像的HttpClient庫,更方便: http://hc.apache.org/httpcomponents-client/index.html

下面是簡單的使用例子: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java

+0

我使用了新的URL(「http:// dev1:8080/data/xml/01423_01.xml」);仍然沒有工作。 :( – joe 2009-09-10 18:26:28

+0

您需要指定完整的網址,如http:// dev1:8080/data/xml/01423_01.xml – MrWhite 2009-09-10 18:45:24

+0

不知何故,http - : -/- /在註釋中缺失 – MrWhite 2009-09-10 18:46:08

相關問題