开发者

How can I post plain text to a WCF service i.e. not wrapped in an XML tag?

开发者 https://www.devze.com 2023-02-02 14:54 出处:网络
Here\'s my problem : I have to build a webservice which accepts plain text in the body of the httppost.

Here's my problem :

I have to build a webservice which accepts plain text in the body of the httppost.

I want to use wcf but it looks like wcf is only made for xml/json.

Does anyone have a method which i can use to post plain text through a http post?

Note that i cannot use soap or wrap the text inside xml tags, i have to follow certain guideli开发者_如何学Cnes to be compatible with existing service consumers.

Thanks in advance.


The trick is you call your method with param Stream xxx

In the iContract

[WebInvoke(Method = "POST",
    UriTemplate = "InterpSvcTest",
    BodyStyle = WebMessageBodyStyle.Bare)] // this is key for combining params with Stream

Stream InterpretORUTest(Stream ORU);

In your WebService class

    public Stream InterpretORUTest(Stream ORUMessageStream)
    {
        string hl7 = StreamToString(ORUMessageStream);

        return StringToStream(hl7);

    }

   public Stream StringToStream(string s)
        {
            return new MemoryStream(Encoding.UTF8.GetBytes(s));

        }

        public string StreamToString(Stream ms)
        {
            try
            {
                using (ms)
                {
                    var sr = new StreamReader(ms);
                    var myStr = sr.ReadToEnd();
                    sr.Dispose();
                    return myStr;
                };
            }
            catch (Exception e)
            {
                c.WriteToLog("StreamToString error - " + e.Message);
                return "";

            }

        }
0

精彩评论

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