[英]call asp.net webmethod in windows project
How do I call this WebMethod
in ASP.NET from a Windows application? 如何从Windows应用程序在ASP.NET中调用此WebMethod
?
I have tried using web request post method, but it is returning the XML of the ASP.NET page. 我尝试使用Web请求发布方法,但是它返回ASP.NET页的XML。
Here is my web method: 这是我的网络方法:
[WebMethod()]
public static string Senddata(string value)
{
return "datareceived" + value;
}
Try this: 尝试这个:
var theWebRequest = HttpWebRequest.Create("http://YOURURL/YOURPAGE.aspx/Senddata");
theWebRequest.Method = "POST";
theWebRequest.ContentType = "application/json; charset=utf-8";
theWebRequest.Headers.Add(HttpRequestHeader.Pragma, "no-cache");
using (var writer = theWebRequest.GetRequestStream())
{
string send = null;
send = "{\"value\":\"test\"}";
var data = Encoding.ASCII.GetBytes(send);
writer.Write(data, 0, data.Length);
}
var theWebResponse = (HttpWebResponse)theWebRequest.GetResponse();
var theResponseStream = new StreamReader(theWebResponse.GetResponseStream());
string result = theResponseStream.ReadToEnd();
// Do something with the result
TextBox1.Text = result;
Note: You need to replace YOURURL
and YOURPAGE
with real values. 注意:您需要用实际值替换YOURURL
和YOURPAGE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.