Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Restful WCF POST method with C#

Tags:

c#

wcf

How can I call a WCF method with the type POST from a C# class?

WCF method

[OperationContract]
[WebInvoke(Method = "POST",
           UriTemplate = "/process",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped)]
MyRespons Process(MyRequest req);

How can I call this from an aspx codebehind?

I've tried to receive the stream with webclient, it works with any get method, but not with POST. The method works in Fiddler and POSTER:

string getDeclarations = string.Format("{0}/process", ServiceBaseAddress);
var proxy = new WebClient();
proxy.DownloadStringCompleted += ProxyDownloadDeclarationsCompleted;
proxy.DownloadStringAsync((new Uri(getDeclarations))); 
like image 784
Gino Avatar asked Dec 13 '25 16:12

Gino


2 Answers

Did you try to add a Service reference to your WCF service in your project where you want to use the WCF service?
To do this, right click your project in the Solution explorer and select Add Service reference. Then type in your URL to your WCF service and you can use it as you referenced any other DLL or project with objects and methods.

like image 199
TimVK Avatar answered Dec 16 '25 09:12

TimVK


You can do this using jQuery. And here is a great example for you to use as well.

This is an example code block.

$.ajax({
    cache: false,
    type: "POST",
    async: false,
    url: /* YOUR URL */,
    data: JSON.stringify(/* YOUR POST DATA */),
    contentType: "application/json",
    dataType: "json",
    success: function (response) {
        /* SUCCESS FUNCTION */
    },
    error: function (error) {
        /* ERROR FUNCTION */
    }
});

Edited

Here is a reference to a Stackoverflow example using WebClient to perform a POST.

like image 34
Mike Perrenoud Avatar answered Dec 16 '25 07:12

Mike Perrenoud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!