开发者

How to past post an XML stream to a wcf http rest service using an HttpWebRequest

开发者 https://www.devze.com 2023-01-23 18:41 出处:网络
I am trying to send an HTTPWebRequest POST XML data to my WCF service. However when setting a breakpoint in my service, it is hit, but my widgetStream is empty when I try to read it.Even if I read it

I am trying to send an HTTPWebRequest POST XML data to my WCF service.

However when setting a breakpoint in my service, it is hit, but my 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".

0

精彩评论

暂无评论...
验证码 换一张
取 消