【问题标题】:XML Response contains HTML encoding on API ResponseXML 响应包含对 API 响应的 HTML 编码
【发布时间】:2011-07-14 19:01:38
【问题描述】:

我创建了一个 WCF 服务,我根据请求将流传递给该服务。客户端代码如下所示:

  FileInfo fo = new FileInfo("c:/Downloads/test.xml");
        StreamWriter wo = fo.CreateText();

        XmlDocument MyXmlDocument = new XmlDocument();
        MyXmlDocument.Load("C:/DataFiles/Integrations/RequestXML.xml");
        byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml);

        Uri uri = new Uri("http://localhost:63899/MyRESTServiceImpl.svc/Receive");

        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);

        Request.ContentLength = RequestBytes.Length;

        Request.Method = "POST";

        Request.ContentType = "text/xml";

        Stream RequestStream = Request.GetRequestStream();
        RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
        RequestStream.Close();

        HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string r = reader.ReadToEnd();
        //XmlDocument ReturnXml = new XmlDocument();
        //ReturnXml.LoadXml(reader.ReadToEnd());
        response.Close();

        wo.Write(r);

现在,我要做的就是处理请求,然后将 XML 直接返回给客户端以进行测试。这里分别是我的 IMyRESTServiceImpl.cs 和 MyRESTServiceImpl.svc.cs 代码:

[ServiceContract]
public interface IMyRESTServiceImpl
{
    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
    Stream Receive(Stream text);
}


public class MyRESTServiceImpl : IMyRESTServiceImpl
{

    public Stream Receive(Stream text)  
        {  
            string stringText = new StreamReader(text).ReadToEnd();  

            return text;
        }   

}

基本上发生的事情是 API 以字符串标签的形式将我的 XML 返回给我,并对 符号 (& gt; & lt;) 使用 HTML 编码。我需要它来将 XML 完全按照发送的方式返回给我。我已经对其进行了调试,并且 XML 在服务器端保持不变,因此在发回时会发生这种情况。关于如何处理这个问题的任何想法?谢谢。

【问题讨论】:

    标签: xml wcf api httpwebrequest httpwebresponse


    【解决方案1】:

    您的实现无法编译 - 该方法被声明为返回 Stream,但它返回的是 String。如果作为字符串返回,它将对 XML 字符进行编码;如果您不想要编码,请将其作为 Stream 或 XmlElement(或 XElement)返回。

    更新示例

    这是为任意 XML 响应返回 Stream 的方法示例:

    [WebGet]
    public Stream GetXML()
    {
        string theXml = @"<products>
      <product name=""bread"" price=""1.33">
        <nutritionalFacts>
          <servings>2</servings>
          <calories>150</calories>
          <totalFat>2</totalFat>
        </nutritionalFacts>
      </product>
      <product name=""milk"" price=""2.99">
        <nutritionalFacts>
          <servings>8</servings>
          <calories>120</calories>
          <totalFat>5</totalFat>
        </nutritionalFacts>
      </product>
    </products>";
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        MemoryStream result = new MemoryStream(Encoding.UTF8.GetBytes(theXml);
        return result;
    }
    

    【讨论】:

    • 是的,但是如何将其作为流发送回来?这就是我卡住的地方。我已经在互联网上搜索了一个工作示例,但一无所获。使用字符串是我可以让 XML 发回的唯一方法。当尝试将其作为 Stream 发送回来时,客户端没有收到任何内容。
    • 我在答案中加了一个例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2013-10-09
    • 2011-06-15
    相关资源
    最近更新 更多