Android的,通过HTTP POST发送XML(SOAP) [英] Android, sending XML via HTTP POST (SOAP)
本文介绍了Android的,通过HTTP POST发送XML(SOAP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过机器人来调用web服务。我需要通过HTTP POST一些XML到URL。 我发现这个剪断发送一个POST,但我不知道如何包含/添加XML数据本身。
公共无效POSTDATA(){
//创建一个新的HttpClient和门柱头球
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://10.10.4.35:53011/);
尝试 {
//添加数据
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(2);
nameValuePairs.add(新BasicNameValuePair(内容类型,应用程序/肥皂+ XML));
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
//凡/如何添加XML数据?
//执行HTTP POST请求
HTT presponse响应= httpclient.execute(httppost);
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
}赶上(IOException异常E){
// TODO自动生成的catch块
}
}
这是我需要模仿完成POST消息:
POST / a8103e90-f1e3-11dd-bfdb-8b1fcff1a110 HTTP / 1.1
主持人:10.10.4.35:53011
内容类型:应用程序/肥皂+ XML
内容长度:602
< XML版本=1.0编码=UTF-8&GT?;
< S12:信封的xmlns:S12 =http://www.w3.org/2003/05/soap-envelope的xmlns:WSA =http://schemas.xmlsoap.org/ws/2004/08/addressing >
< S12:头>
< WSA:邮件ID>瓮:UUID:fc061d40-3d63-11df-bfba-62764ccc0e48< / WSA:邮件ID>
&LT; WSA:作用&gt;的http://schemas.xmlsoap.org/ws/2004/09/transfer/Get< / WSA:作用&gt;
&LT; WSA:至大于瓮:UUID:a8103e90-f1e3-11dd-bfdb-8b1fcff1a110&LT; / WSA:到大于
&LT; WSA:的ReplyTo&GT;
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
&LT; / WSA:的ReplyTo&GT;
&LT; / S12:头&GT;
&LT; S12:车身/&GT;
&LT; / S12:信封&GT;
解决方案
- 首先,你可以创建这个SOAP请求,并代替用户提供的值在运行时在此模板来创建一个有效的请求字符串的模板。
- 在包装这个字符串中StringEntity并设置其内容类型为text / xml的
- 设置在SOAP请求这个实体。
是这样的:
HttpPost httppost =新HttpPost(SERVICE_EPR);
StringEntity SE =新StringEntity(SOA prequestXML,HTTP.UTF_8);
se.setContentType(为text / xml);
httppost.setHeader(内容类型,应用程序/肥皂+ XML;字符集= UTF-8);
httppost.setEntity(SE);
HttpClient的HttpClient的=新DefaultHttpClient();
BasicHtt presponse HTT presponse =
(BasicHtt presponse)httpclient.execute(httppost);
(的HTTPStatus,HTT presponse.getStatusLine()的toString())response.put;
I would like to invoke a webservice via Android. I need to POST some XML to a URL via HTTP. I found this snipped for sending a POST, but i dont know how to include/add the XML data itself.
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
}
}
This is the complete POST message that i need to imitate:
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>
解决方案
- First, you can create a String template for this SOAP request and substitute user-supplied values at runtime in this template to create a valid request.
- Wrap this string in a StringEntity and set its content type as text/xml
- Set this entity in the SOAP request.
Something like:
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());
这篇关于Android的,通过HTTP POST发送XML(SOAP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文