在Windows项目中调用asp.net webmethod [英] call asp.net webmethod in windows project

查看:107
本文介绍了在Windows项目中调用asp.net webmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Windows应用程序在ASP.NET中将此 WebMethod 称为?

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

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

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

这是我的网络方法:

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

注意:您需要将 YOURURL YOURPAGE 替换为实际值.

Note: You need to replace YOURURL and YOURPAGE with real values.

这篇关于在Windows项目中调用asp.net webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆