如何從Windows應用程序調用ASP.NET中的這個WebMethod
?在windows項目中調用asp.net webmethod
我試過使用web請求發佈方法,但它返回的是ASP.NET頁面的XML。
這裏是我的Web方法:
[WebMethod()]
public static string Senddata(string value)
{
return "datareceived" + value;
}
如何從Windows應用程序調用ASP.NET中的這個WebMethod
?在windows項目中調用asp.net webmethod
我試過使用web請求發佈方法,但它返回的是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
。
右鍵單擊解決方案資源管理器中的項目,添加服務引用,指定您的Web服務的URL。這將在你的項目中創建一個代理類,用它來調用你的方法。 – Habib
habib它是一個方法在asp.net項目 – L202