gpt4 book ai didi

c# - 在windows项目中调用asp.net webmethod

转载 作者:太空狗 更新时间:2023-10-30 00:52:50 25 4
gpt4 key购买 nike

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

我试过使用 web 请求 post 方法,但它返回的是 ASP.NET 页面的 XML。

这是我的网络方法:

[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;

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

关于c# - 在windows项目中调用asp.net webmethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19597291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com