+-
用C#调用Restful WCF POST方法
如何从C#类调用类型为POST的WCF方法?

WCF方法

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

我怎样才能从aspx代码背后调用它呢?

我试图通过webclient接收流,它可以与任何get方法一起使用,但不能与POST一起使用.该方法适用于Fiddler和POSTER:

string getDeclarations = string.Format("{0}/process", ServiceBaseAddress);
var proxy = new WebClient();
proxy.DownloadStringCompleted += ProxyDownloadDeclarationsCompleted;
proxy.DownloadStringAsync((new Uri(getDeclarations))); 
最佳答案
您可以使用jQuery完成此操作.这也是 great example供您使用.

这是一个示例代码块.

$.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 */
    }
});

已编辑

这是使用WebClient执行POST的Stackoverflow示例reference.

点击查看更多相关文章

转载注明原文:用C#调用Restful WCF POST方法 - 乐贴网