C# 使用ContentType时请求不正确;text/xml“;WCF REST中的XML和预定义XML
我需要编写一个REST服务来接受来自客户端应用程序的XML文档。我没有访问客户端应用程序的权限,无法对其进行更改 它使用内容类型为text/xml的HTTP POST发送文档;charset=“UTF-8” 我试过两个不同的运营合同,他们都有不同的问题 首先,我的主机代码:C# 使用ContentType时请求不正确;text/xml“;WCF REST中的XML和预定义XML,c#,xml,wcf,rest,C#,Xml,Wcf,Rest,我需要编写一个REST服务来接受来自客户端应用程序的XML文档。我没有访问客户端应用程序的权限,无法对其进行更改 它使用内容类型为text/xml的HTTP POST发送文档;charset=“UTF-8” 我试过两个不同的运营合同,他们都有不同的问题 首先,我的主机代码: private static WebServiceHost _host; public static void ConnectToHost() { string url = Conf
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);
[OperationContract]
[WebInvoke(UriTemplate = "SendXML", Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
XElement SendXML(XElement xml);
我可以使用“text/plain”的内容类型接收XML文件,但如果我将其切换为“text/XML”,这是客户端将要发送的内容,则会收到400个错误请求
如果我用这个
[OperationContract]
[WebInvoke(UriTemplate = "SendText")]
Stream SendText(Stream s);
[OperationContract]
[WebInvoke(UriTemplate = "SendXML", Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
XElement SendXML(XElement xml);
然后,它可以处理“text/xml”,但由于xml在根之外有一个DOCTYPE元素,因此请求失败。我无法更改此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>
///这里的数据
//这里的数据
以下是将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);