1

クライアント アプリケーションから XML ドキュメントを受け入れる REST サービスを作成する必要があります。クライアント アプリケーションへのアクセス権がなく、変更できません。

コンテンツ タイプが text/xml の HTTP POST を使用してドキュメントを送信します。charset="UTF-8".

2 つの異なるオペレーション コントラクトを試しましたが、どちらも異なる問題があります...

まず私のホストコード:

    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 Bad Request が返されます。

これを使えば…

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

次に、「text/xml」で動作しますが、XML のルートの外側に DOCTYPE 要素があるため、400 Bad Request で失敗します。この 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 に答える