gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:48:47 26 4
gpt4 key购买 nike

我想通过 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com