2012-10-13 99 views
1

我需要使用POST將XML文件發送到Web服務。我有一個客戶端應用程序,它創建一個XML文件,該文件存儲發送到Web應用程序所需的所有信息,但我不知道如何去發送它。通過POST將XML文件發送到Java中的RESTful服務

我的XML看起來是這樣的:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<Comment> 
    <Poll_ID>2</Poll_ID> 
    <Name>wpoon</Name> 
    <Text>asdasdas</Text> 
    <Timestamp>2012-10-14T10:30:25</Timestamp> 
</Comment> 

而且RESTful服務,我將它發送到具有URL:

http://localhost:8080/TESTINGrestful/rest/polls/comment 

誰能告訴我如何做到這一點,任何幫助將不勝感激。

回答

12

有一個很好的例子here Apache的HttpClient

DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost postRequest = new HttpPost("http://localhost:8080/TESTINGrestful/rest/polls/comment"); 
StringEntity input = new StringEntity("<Comment>...</Comment>"); 
input.setContentType("text/xml"); 
postRequest.setEntity(input); 
HttpResponse response = httpClient.execute(postRequest); 
+1

那麼你必須將XML轉換爲字符串,並通過你寫有代碼發送? – SNpn

+0

從頭開始計算出來:) – SNpn

相關問題