我需要编写一个从客户端应用程序接受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 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>
答案 0 :(得分: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);