1

我需要编写一个接受来自客户端应用程序的 XML 文档的 REST 服务。我无权访问客户端应用程序,也无法更改。

它使用内容类型为 text/xml 的 HTTP POST 发送文档;字符集=“UTF-8”。

我尝试了两种不同的操作合同,它们都有不同的问题......

首先我的主机代码:

    private static WebServiceHost _host;

    public static void ConnectToHost()
    {
        string url = ConfigHelper.GetValue("WebService.config", "WebServiceURL");
        Uri baseAddress = new Uri(url);
        Type instanceType = typeof(CXMLService);
        _host = new WebServiceHost(instanceType, baseAddress);
        Type contractType = typeof(ICXMLService);

        ServiceEndpoint endpoint = _host.AddServiceEndpoint(contractType, new WebHttpBinding(), "Web");            
        endpoint.Behaviors.Add(new WebHttpBehavior());            
        _host.Open();
    }

如果我用这个...

    [OperationContract]
    [WebInvoke(UriTemplate = "SendText")]
    Stream SendText(Stream s);

我可以使用“text/plain”的内容类型接收 XML 文件,但如果我将其切换到客户端将发送的“text/xml”,我会收到 400 错误请求。

如果我用这个...

    [OperationContract]
    [WebInvoke(UriTemplate = "SendXML", Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare, 
        RequestFormat = WebMessageFormat.Xml, 
        ResponseFormat = WebMessageFormat.Xml)]
    XElement SendXML(XElement xml);

然后它与“text/xml”一起工作,但由于 XML 在根之外有一个 DOCTYPE 元素而失败并出现 400 错误请求。我无法更改此 XML 文件。这是文件的示例...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.2.024/cXML.dtd">
<cXML payloadID="32232995@ariba.acme.com"
      timestamp="2000-10-12T18:39:09-08:00" xml:lang="en-US">
    <Header>
         /// data here
    </Header>
    <Request deploymentMode="test">
       // data here
    </Request>
</cXML>
4

1 回答 1

3

这是将 xml 文档流式传输到 WCF 服务的基本方法。

合同:

[OperationContract]
    [WebInvoke(Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml,
        UriTemplate = "ProfileRequest")]
    Stream ProfileRequest(Stream value);

服务:

    public Stream ProfileRequest(Stream value)
    {
        StreamReader reader = new StreamReader(value);
        string text = reader.ReadToEnd();

        XDocument post = XDocument.Parse(text);
        XDocument response = ProfileRequest(post);

        return new MemoryStream(Encoding.UTF8.GetBytes(response.ToString()));
    }

测试控制台:

            string filePath = "C:\someFile.xml";

            XDocument testDoc = XDocument.Load(filePath);

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);
            string newDoc = xDoc.InnerXml.ToString();

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(testDoc.ToString());

            string localProfile = "http://localhost/WcfService/Service1.svc/ProfileRequest";

            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(localProfile);
            webrequest.Method = "POST";
            webrequest.ContentType = "text/xml";
            webrequest.ContentLength = data.Length;
            Stream newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
            string strResult = string.Empty;
            Encoding enc = System.Text.Encoding.GetEncoding("UTF-8");
            StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
            strResult = loResponseStream.ReadToEnd();
            loResponseStream.Close();
            webresponse.Close();

            Console.Write(strResult);
于 2013-06-28T18:00:55.440 回答