2013-10-25 58 views
2

如何從Windows應用程序調用ASP.NET中的這個WebMethod在windows項目中調用asp.net webmethod

我試過使用web請求發佈方法,但它返回的是ASP.NET頁面的XML。

這裏是我的Web方法:

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

右鍵單擊解決方案資源管理器中的項目,添加服務引用,指定您的Web服務的URL。這將在你的項目中創建一個代理類,用它來調用你的方法。 – Habib

+0

habib它是一個方法在asp.net項目 – L202

回答

4

試試這個:

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

+0

非常感謝它真的有效..但如果我們不知道參數是「價值」呢? – L202

+0

據我所知,參數的名稱必須匹配,什麼情況下你會不知道參數名稱? –

+0

不,我不知道這是我甚至不知道方法名稱的問題。我有理由給「somewebsite.com/somthing.aspx」 – L202

相關問題