Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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...

like image 707
Stavros Avatar asked Oct 10 '10 12:10

Stavros


People also ask

How do you call a webservice?

To call a Web service programmaticallyUse the Web reference name (or server name) as the namespace, and the name of its . WSDL file (or the service name) as the proxy class. The following code example calls a Web service method to get a string value. Web service variable names and syntax are highlighted.

How do I call a webservice in C#?

In the Project Type pane, select a Visual C# application. In the Templates pane, select a Windows Forms Application. Enter a project name such as CallingServiceExample , and browse to a storage location. In the Solution Explorer, right-click the project name and click Add Service Reference.

What is Web Services C?

Web Services are self-describing, that means ASP.NET automatically provides all the information the client needs to consume a service as a WSDL document. The WSDL document tells a client what methods are present in a web service, what parameters and return values each method uses and how to communicate with them.

How do I call a webservice in Visual Studio?

In Visual Studio, create a new project using the ASP.NET Web Application (. NET Framework) template, and select the Empty template when prompted. Type a name and create the project. In Solution Explorer, right-click the project node, choose Add > New Item, and then choose Web Service (ASMX).


2 Answers

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.

like image 185
Philip Rieck Avatar answered Sep 19 '22 12:09

Philip Rieck


I would explore using ASP.NET MVC for your web service. You can provide parameters via the standard form parameters and return the result as JSON.

[HttpPost]
public ActionResult MyPostAction( string foo, string bar )
{
     ...
     return Json( new { Value = "baz" } );
}

In your client, use the HttpWebRequest

var request = WebRequest.Create( "/controller/mypostaction" );
request.Method = "POST";
var data = string.Format( "foo={0}&bar={1}", foo, bar );
using (var writer = new StreamWriter( request.GetRequestStream() ))
{
    writer.WriteLine( data );
}
var response = request.GetResponse();
var serializer = new DataContractJsonSerializer(typeof(PostActionResult));
var result = serializer.ReadObject( response.GetResponseStream() )
                 as PostActionResult;

where you have

public class PostActionResult
{
     public string Value { get; set; }
}
like image 25
tvanfosson Avatar answered Sep 19 '22 12:09

tvanfosson