首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在windows项目中调用asp.net webmethod

问在windows项目中调用asp.net webmethod
EN

Stack Overflow用户
提问于 2013-10-25 18:44:28
回答 1查看 8.5K关注 0票数 4

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

我尝试过使用方法,但它正在返回ASP.NET页面的XML。

以下是我的web方法:

代码语言:javascript
复制
[WebMethod()]
public static string Senddata(string value)
{
    return "datareceived" + value;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-25 19:03:23

试试这个:

代码语言:javascript
复制
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;

注意:您需要用实值替换YOURURL和YOURPAGE。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19597291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档