call asp.net webmethod in windows project

前端 未结 1 1325

How do I call this WebMethod in ASP.NET from a Windows application?

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

1条回答
  •  爱一瞬间的悲伤
    2021-01-13 17:02

    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.

    0 讨论(0)
提交回复
热议问题