【问题标题】:call asp.net webmethod in windows project在 windows 项目中调用 asp.net webmethod
【发布时间】:2013-10-25 18:44:28
【问题描述】:

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

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

这是我的网络方法:

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

【问题讨论】:

  • 在解决方案资源管理器中右键单击项目,添加服务引用,指定您的 Web 服务的 URL。这将在您的项目中创建一个代理类,使用它来调用您的方法。
  • habib 是asp.net项目中的一个方法

标签: c# asp.net web-services webmethod


【解决方案1】:

试试这个:

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 替换为实际值。

【讨论】:

  • 非常感谢它确实有效.. 但是如果我们不知道参数是“值”怎么办?
  • 据我所知,参数名称必须匹配,什么场景你会不知道参数名称?
  • 不,我不知道这是我什至不知道方法名称的问题。我只是得到了“somewebsite.com/somthing.aspx”
猜你喜欢
  • 2013-10-05
  • 1970-01-01
  • 2013-10-07
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 2023-03-26
相关资源
最近更新 更多