L202 L202 - 7 months ago 69
ASP.NET (C#) Question

call asp.net webmethod in windows project

How do I call this

in ASP.NET from a Windows application?

I have tried using web request post method, but it is returning the XML of the ASP.NET page.

Here is my web method:

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.