[英]Consume JSON WCF services in C#
I want to consume my json wcf web service from the code behind of my page: 我想从页面后面的代码中使用json wcf Web服务:
Default.aspx.cs Default.aspx.cs
string result = url + "/ExecuteAction?callback=?";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(result);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
action = "HelloWorld",
args = "Nabila"
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var response = streamReader.ReadToEnd();
}
My service: 我的服务:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string ExecuteAction(string action, string args)
{
String JSONResult = String.Empty;
MethodInfo action = services.GetMethod(action, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
JSONResult =(String)action.Invoke(null, new object[] { args });
}
services.cs services.cs
public static string HelloWorld(string msg)
{
return JsonConvert.SerializeObject("Hello World"+msg);
}
I get the following exception : 我得到以下异常:
consume json wcf web service from c# The remote server returned an error: (405) Method Not Allowed.
从c#消费json wcf Web服务远程服务器返回错误:(405)不允许使用方法。 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,EventArgs e)
Note: It works when I consume my web service using javascript: 注意:当我使用javascript使用Web服务时,它可以工作:
$.ajax({
type: "POST",
crossDomain: true,
timeout: 10000,
url: getUrl() + "ExecuteAction?callback=?",
data: { action: "HelloWorld", args: 'test'},
contentType: "application/json; charset=utf-8",
dataType: "jsonp"
}).done(function (msg) {alert("success");}).fail(function () {alert("error");});
Could you please help me. 请你帮助我好吗。
Thanks. 谢谢。
Nabila. 纳比拉。
I am observing that your problem is with DataType . 我发现您的问题出在DataType上 。 In your Javascript call, you are setting datType as jsonp
在您的Javascript调用中,您将datType设置为jsonp
dataType: "jsonp"
And in your Default.aspx.cs file you are sending plain JSON. 在Default.aspx.cs文件中,您将发送纯JSON。 There is difference between JSON and JSONP .
JSON和JSONP之间有区别 。
Try changing JSON type in your Deafult.aspx.cs file and then run. 尝试在Deafult.aspx.cs文件中更改JSON类型,然后运行。 This might resolve your problem.
这可能会解决您的问题。
EDIT: Change your Contract as: 编辑:将您的合同更改为:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "json")]
public string ExecuteAction(JSONRequest request)
{
String JSONResult = String.Empty;
MethodInfo action = services.GetMethod(action, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
JSONResult =(String)action.Invoke(null, new object[] { args });
}
[DataContract]
public class JSONRequest
{
[DataMember]
public string Action {get; set;}
[DataMember]
public string Args {get; set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.