Sending and receiving SOAP messages

前端 未结 4 1323
一整个雨季
一整个雨季 2021-02-06 08:39

I am writing a web service client in C# and do not want to create and serialize/deserialize objects, but rather send and receive raw XML.

Is this possible in C#?

4条回答
  •  粉色の甜心
    2021-02-06 09:20

    You can use the System.Net classes, such as HttpWebRequest and HttpWebResponse to read and write directly to an HTTP connection.

    Here's a basic (off-the-cuff, not compiled, non-error-checking, grossly oversimplified) example. May not be 100% correct, but at least will give you an idea of how it works:

    HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create(url);
    req.ContentLength = content.Length;
    req.Method = "POST";
    req.GetRequestStream().Write(Encoding.ASCII.GetBytes(content), 0, content.Length);
    HttpWebResponse resp = (HttpWebResponse) req.getResponse();
    //Read resp.GetResponseStream() and do something with it...
    

    This approach works well. But chances are whatever you need to do can be accomplished by inheriting the existing proxy classes and overriding the members you need to have behave differently. This type of thing is best reserved for when you have no other choice, which is not very often in my experience.

提交回复
热议问题