在windows项目中调用asp.net webmethod

L20*_*202 4 c# asp.net web-services webmethod

如何WebMethod从Windows应用程序在ASP.NET中调用它?

我尝试过使用web request post方法,但它返回了ASP.NET页面的XML.

这是我的网络方法:

[WebMethod()]
public static string Senddata(string value)
{
    return "datareceived" + value;
}
Run Code Online (Sandbox Code Playgroud)

Kar*_*son 6

试试这个:

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;
Run Code Online (Sandbox Code Playgroud)

注意:您需要替换YOURURLYOURPAGE使用实际值.