4

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

我曾尝试使用 Web 请求发布方法,但它返回的是 ASP.NET 页面的 XML。

这是我的网络方法:

[WebMethod()]
public static string Senddata(string value)
{
    return "datareceived" + value;
}
4

1 回答 1

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;

注意:您需要将YOURURLand替换YOURPAGE为实际值。

于 2013-10-25T19:03:23.030 回答