[英]initiating http post request from html form and receiving that request in java
[英]Receiving XML from HTTP Post
所以我有以下代碼:
public void SendToApplication(HttpServletRequest request) throws IOException, TransformerException {
BufferedReader br = new BufferedReader(new FileReader(new File("CreatePoll.xml")));
String line;
StringBuilder sb = new StringBuilder();
while((line=br.readLine())!= null) sb.append(line.trim());
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://localhost:8080/cs9322.simple.rest.doodle/rest/polls/comment");
StringEntity input = new StringEntity(sb.toString());
input.setContentType("text/xml");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
}
讀取XML文件(CreatePoll.xml),
<Comment xmlns:xs="http://localhost:8080/cs9322.simple.rest.doodle/CommentSchema">
<Poll_ID>2</Poll_ID>
<Name>Bob</Name>
<Text>testing junk</Text>
<Timestamp>2012-10-14T12:37:04</Timestamp>
</Comment>
並將其發布到Web服務,我現在遇到的問題是嘗試在發送后從Web服務接收XML響應。 我打算接收的XML是:
<comment>
<address>
</address>
</comment>
有人可以在這里幫助我,不勝感激!
如果您使用Apache Commons IO ,則可以使用IOUtils
類從HttpEntity
讀取輸入流。 使用twitter rest API的示例:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://search.twitter.com/search.atom?q=elkstein&count=5");
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String body = IOUtils.toString(entity.getContent(), "UTF-8");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.