1

我继承了一个由客户端发布的 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 - 但即使上面未注释的代码插入它仍然失败。

为任何指针干杯,

射线

4

0 回答 0