我该如何在Windows应用程序中从ASP.NET调用这个WebMethod
?
我尝试使用Web请求的POST方法,但它返回了ASP.NET页面的XML。
这是我的Web方法:
[WebMethod()]
public static string Senddata(string value)
{
return "datareceived" + value;
}
我该如何在Windows应用程序中从ASP.NET调用这个WebMethod
?
我尝试使用Web请求的POST方法,但它返回了ASP.NET页面的XML。
这是我的Web方法:
[WebMethod()]
public static string Senddata(string value)
{
return "datareceived" + value;
}
试试这个:
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;
YOURURL
和YOURPAGE
。