Search code examples
c#web-services.net-2.0

How can I call a webservice from C# with HTTP POST


I want to write a c# class that would create a connection to a webservice running to www.temp.com, send 2 string params to the method DoSomething and get the string result. I don't want to use wsdl. Since I know the params of the webservice, I just want to make a simple call.

I guess there should be an easy and simple way to do that in .Net 2, but I couldn't find any example...


Solution

  • If this "webservice" is a simple HTTP GET, you can use WebRequest:

    WebRequest request = WebRequest.Create("http://www.temp.com/?param1=x&param2=y");
    request.Method="GET";
    WebResponse response = request.GetResponse();
    

    From there you can look at response.GetResponseStream for the output. You can hit a POST service the same way.

    However, if this is a SOAP webservice, it's not quite that easy. Depending on the security and options of the webservice, sometimes you can take an already formed request and use it as a template - replace the param values and send it (using webrequest), then parse the SOAP response manually... but in that case you're looking at lots of extra work an may as well just use wsdl.exe to generate proxies.