简体   繁体   English

如何使用HttpWebRequest将XML流过帐到wcf http rest服务

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

I am trying to send an HTTPWebRequest POST XML data to my WCF service. 我正在尝试将HTTPWebRequest POST XML数据发送到WCF服务。
However when setting a breakpoint in my service, it is hit, but my widgetStream is empty when I try to read it. 但是,在服务中设置断点时会命中它,但是当我尝试读取它时, widgetStream为空。 Even if I read it from a StreamReader . 即使我从StreamReader读取了它。

Does anyone know what I am doing wrong? 有人知道我在做什么错吗?

My WCF service looks like this: 我的WCF服务如下所示:

    [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: 我的客户端正在尝试将XML资源连接并发布到HTTP URL,如下所示:

public static void CreateWidget(Widget myWidget)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:29858/myservice/widgets");
    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. 我没有尝试使用HTTPWebRequest对象,但可以使用Fiddler将原始XML发布到服务。

You might need to change Content-Type on the client to "application/xml" rather than "text/xml". 您可能需要将客户端上的Content-Type更改为“ application / xml”,而不是“ text / xml”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM