我继承了一个由客户端发布的ASPX页面。我不能改变客户端。我想重做这个作为WCF Rest服务。 ASPX页面从Page_load事件
中的请求中检索XML数据经过一段时间的阅读后,我有一个WCF Rest服务设置,它使用相同的测试数据,但前提是我的测试代码将contentType设置为:
request.ContentType = @"application/x-www-form-urlencode";
打电话给我的客户将使用:
request.ContentType = @"text/xml";
此操作因 400错误而失败。
这是我的界面:
public interface ISmsReceive {
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "xml")]
string Receive(Stream ID);
}
这是一个有效的示例调用:
string s = "http://localhost:1773/SmsReceive.svc/xml";
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<accountrequest>" + // xmlns=\"http://tempuri.org/\">" +
"<accountprocessinboundmessage>" +
"<inboundnumber><![CDATA[123]]></inboundnumber>" +
"<dateinsert><![CDATA[1/1/2003 12:00:00 PM]]></dateinsert>" +
"<inboundcli><![CDATA[+123]]></inboundcli>" +
"<message><![CDATA[blah blah]]></message>" +
"</accountprocessinboundmessage>" +
"</accountrequest>";
WebRequest request = WebRequest.Create(s);
request.Method = "POST";
// Works with this
request.ContentType = @"application/x-www-form-urlencode";
// Fails with this
//request.ContentType = @"text/xml";
xmlStream = (request.GetRequestStream());
StreamWriter writer = new StreamWriter(xmlStream);
writer.Write(xmlString);
writer.Flush();
writer.Close();
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
String responseString = reader.ReadToEnd();
我在一篇文章中读到你必须在XML中定义一个xmlns - 但即使上面的代码没有注释,插入它仍然会失败。
欢呼任何指针,
雷