2013-10-11 138 views
9

我有Restful WebServices,並且我發送POST和GET HTTP請求,如何在httpURLConection中用JAVA發送PUT和DELTE請求HTTP。如何在HTTP中發送PUT,DELETE HTTP請求中的HttpURLConnection

+0

至於HttpURLConnection的可以使用[DavidWebb](https://github.com/hgoebl/DavidWebb)稱爲一個微小的庫天然使用的替代方法。在那裏你可以找到類似庫的列表。本地使用HttpURLConnection非常麻煩並且容易出錯。 – hgoebl

回答

23

PUT

URL url = null; 
try { 
    url = new URL("http://localhost:8080/putservice"); 
} catch (MalformedURLException exception) { 
    exception.printStackTrace(); 
} 
HttpURLConnection httpURLConnection = null; 
DataOutputStream dataOutputStream = null; 
try { 
    httpURLConnection = (HttpURLConnection) url.openConnection(); 
    httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    httpURLConnection.setRequestMethod("PUT"); 
    httpURLConnection.setDoInput(true); 
    httpURLConnection.setDoOutput(true); 
    dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream()); 
    dataOutputStream.write("hello"); 
} catch (IOException exception) { 
    exception.printStackTrace(); 
} finally { 
    if (dataOutputStream != null) { 
     try { 
      dataOutputStream.flush(); 
      dataOutputStream.close(); 
     } catch (IOException exception) { 
      exception.printStackTrace(); 
     } 
    } 
    if (httpsURLConnection != null) { 
     httpsURLConnection.disconnect(); 
    } 
} 

DELETE

URL url = null; 
try { 
    url = new URL("http://localhost:8080/deleteservice"); 
} catch (MalformedURLException exception) { 
    exception.printStackTrace(); 
} 
HttpURLConnection httpURLConnection = null; 
try { 
    httpURLConnection = (HttpURLConnection) url.openConnection(); 
    httpURLConnection.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
    httpURLConnection.setRequestMethod("DELETE"); 
    System.out.println(httpURLConnection.getResponseCode()); 
} catch (IOException exception) { 
    exception.printStackTrace(); 
} finally {   
    if (httpURLConnection != null) { 
     httpURLConnection.disconnect(); 
    } 
} 
+0

謝謝你的完美!只有我需要方法刪除。 – user2816207

+0

不需要調用htpURLConnection.connect()?這個功能是什麼? –

-1

可以覆蓋

protected void doPut(HttpServletRequest req, 
         HttpServletResponse resp) throws ServletException, IOException; 

執行的HTTP PUT操作;默認實現報告 HTTP BAD_REQUEST錯誤。 PUT操作類似於通過FTP發送 文件。覆蓋此方法的Servlet編寫者必須尊重 與請求一起發送的任何Content- *標頭。 (這些標頭包括內容長度,內容類型,內容傳輸編碼,內容編碼,內容基礎,內容語言,內容位置,內容MD5和內容範圍。)如果子類無法兌現 內容標題,那麼它必須發出錯誤響應(501)並放棄請求的 。有關更多信息,請參閱HTTP 1.1 RFC。

此方法不需要是「安全的」或「冪等的」。 通過PUT請求的操作可能會產生副作用,因此 用戶可能會被追究責任。雖然不是必需的,但重寫此方法的servlet編寫器 可能希望將受影響的URI 的副本保存在臨時存儲中。

protected void doDelete(HttpServletRequest req, 
         HttpServletResponse resp) throws ServletException, IOException 

執行的HTTP DELETE操作;默認實現報告 有HTTP BAD_REQUEST錯誤。 DELETE操作允許客戶端請求從服務器中刪除URI。此方法不需要 爲「安全」或「冪等」。通過 請求的操作DELETE可能會產生用戶可能被追究責任的副作用。 雖然不是必需的,但對此方法進行子類化的servlet編寫人員可能希望將受影響的URI的副本保存在臨時存儲中。

+0

這些都是Servlet API方法,但OP詢問HTTP客戶端我認爲 –

+0

也許,但我明白他已經實現了一個管理doGet和doPut的servlet,並且想知道如何執行其他兩個操作 – RamonBoza

+0

我有getter和postT,我需要實現刪除,並把方法,但是不要來,因爲tenngo一個輸入參數可以幫助我? - @MatthewGilliard – user2816207

4

如果我使用@BartekM刪除請求,它得到此異常:

java.net.ProtocolException: DELETE does not support writing 

要剛修好它,我刪除該指令:

// httpURLConnection.setDoOutput(true); 

源:Sending HTTP DELETE request in Android