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)));
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.
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 */
}
});
Here is a reference to a Stackoverflow example using WebClient to perform a POST.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With