I am trying to send an HTTPWebRequest
POST XML data to my WCF service.
widgetStream
is empty when I try to read it. Even if I read it from a StreamReader
.
Does anyone know what I am doing wrong?
My WCF service looks like this:
[WebInvoke(UriTemplate = "widgets", Method = "POST"]
public void CreateWidget(Stream widgetStream)
{
try
{
XElement e = XElement.Load(widgeStream);
//...
}
catch (Exception ex)
throw;
}
}
My client is trying to connect and post an XML resource to an HTTP URL like so:
public static void CreateWidget(Widget myWidget)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:29858/myservice/widge开发者_如何学JAVAts");
request.Method = "POST";
request.ContentType = "text/xml";
string xml = myWidget.ToXML().ToString();
request.ContentLength = xml.Length;
Stream s = request.GetRequestStream();
StreamWriter sr = new StreamWriter(s);
sr.Write(xml);
sr.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
If anyone could please help that would be appreciated!
[WebInvoke(UriTemplate = "widgets", Method = "POST", RequestFormat=WebMessageFormat.Xml, BodyStyle=WebMessageBodyStyle.Bare]
[XmlSerializerFormat]
public void CreateWidget(XElement widget){...}
Try this on your service. I just ran into a similar issue this week. I haven't tried using the HTTPWebRequest object but I can post raw XML to the service using Fiddler.
You might need to change Content-Type on the client to "application/xml" rather than "text/xml".
精彩评论