我正在开发这个应用程序,我需要将XML文件发送到Web服务并从Web服务中读取它,并从Web服务应用业务逻辑,它将发送XML作为响应。
问题是我确实知道如何读取webservice发送的响应,但我不知道如何检索请求发送到webservice的XML,变量总是为空。如果有人可以帮助我,我会很高兴。
请求方法:
(...)
using (var client = new HttpClient())
{
var XMLRequest = Util.BuildXML.CreateRequestXML();
string url = string.Format(WEB_API_HOST + "/Application/MyMethod/");
HttpRequestMessage httpRequest = new HttpRequestMessage()
{
RequestUri = new Uri(url, UriKind.Absolute),
Method = HttpMethod.Post,
Content = new StringContent(XMLRequest.ToString(), Encoding.UTF8, "text/xml")
};
var task = client.SendAsync(httpRequest).ContinueWith((taskwithmsg) =>
{
var response = taskwithmsg.Result;
if (response.IsSuccessStatusCode)
{
var xmlResponse = response.Content.ReadAsStringAsync().Result;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlResponse);
var test = doc.SelectSingleNode("RESPONSE/...").InnerText;
}
else
{
var contentTask = response.Content.ReadAsStringAsync().Result;
throw new Exception(contentTask);
}
});
task.Wait();
}
(...)
Webservice(应该接收XML并读取它)
[AcceptVerbs("GET", "POST")]
public HttpResponseMessage MyMethod(XmlDocument doc)
{
// Any doc.SelectSingleNode() wouldn't work, the doc variable is null.
var response = new StringBuilder();
response.Append("<?xml version=\"1.0\" standalone=\"yes\"?>");
response.Append("<RESPONSE>");
(...)
response.Append("</RESPONSE>");
var xmlResponse = new HttpResponseMessage()
{
Content = new StringContent(response.ToString(), Encoding.UTF8, "text/xml")
};
return xmlResponse;
}
那么如何在“MyMethod”方法中阅读xml?
非常感谢!