xml - Android,通过 HTTP POST (SOAP) 发送 XML

标签 xml android http soap post

我想通过 Android 调用网络服务。我需要通过 HTTP 将一些 XML 发布到 URL。 我发现这是为了发送 POST 而剪掉的,但我不知道如何包含/添加 XML 数据本身。

public void postData() {
         // Create a new HttpClient and Post Header  
         HttpClient httpclient = new DefaultHttpClient();  
         HttpPost httppost = new HttpPost("http://10.10.4.35:53011/");

         try {  
             // Add your data  
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
             nameValuePairs.add(new BasicNameValuePair("Content-Type", "application/soap+xml"));               
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
                 // Where/how to add the XML data?


             // Execute HTTP Post Request  
             HttpResponse response = httpclient.execute(httppost);  

         } catch (ClientProtocolException e) {  
             // TODO Auto-generated catch block  
         } catch (IOException e) {  
             // TODO Auto-generated catch block  
         }  
     }

这是我需要模仿的完整 POST 消息:

POST /a8103e90-f1e3-11dd-bfdb-8b1fcff1a110 HTTP/1.1
Host: 10.10.4.35:53011
Content-Type: application/soap+xml
Content-Length: 602

<?xml version='1.0' encoding='UTF-8' ?>
<s12:Envelope xmlns:s12="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
  <s12:Header>
    <wsa:MessageID>urn:uuid:fc061d40-3d63-11df-bfba-62764ccc0e48</wsa:MessageID>
    <wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/Get</wsa:Action>
    <wsa:To>urn:uuid:a8103e90-f1e3-11dd-bfdb-8b1fcff1a110</wsa:To>
    <wsa:ReplyTo>
      <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
    </wsa:ReplyTo>
  </s12:Header>
  <s12:Body />
</s12:Envelope>

最佳答案

  1. 首先,您可以为此 SOAP 请求创建一个字符串模板,并在运行时在此模板中替换用户提供的值,以创建一个有效的请求。
  2. 将此字符串包装在 StringEntity 中并将其内容类型设置为 text/xml
  3. 在 SOAP 请求中设置此实体。

类似:

HttpPost httppost = new HttpPost(SERVICE_EPR);          
StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);

se.setContentType("text/xml");  
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(se);  

HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse = 
    (BasicHttpResponse) httpclient.execute(httppost);

response.put("HTTPStatus",httpResponse.getStatusLine().toString());

关于xml - Android,通过 HTTP POST (SOAP) 发送 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2559948/

相关文章:

java - 查询以 1 行代码获取电子邮件、联系人姓名和电话号码

mysql - 详细的 RoR 应用程序...带有 MySQL 的 XML 或仅 MySQL

java - 通过 Jersey 的 JAX-RS -> HTTP 正文中没有返回内容(方法完成没有错误)

ios - 如何使用公司代理为 iOS 模拟器设置 Charles 代理

java - Android 截击 : Cannot pass data to server

http - 如何从命令行发送 HTTPS 0.9 请求?

java - fragment xml 错误?

java - JAXB 空元素解码

javascript - 嵌套的 PinchGestureHandler 和 PanGestureHandler 在 Android 中不起作用

手机关机时 Android 应用程序从 LG Allys 中删除