发布期间的WCF服务呼叫

时间:2011-04-08 04:40:57

标签: asp.net wcf wcf-rest

我已经给出了

[WebGet(UriTemplate = "/{year}/{issue}/{article}")] 

Article GetArticle(string year, string issue, string article); 

[OperationContract] 

[WebInvoke(UriTemplate = "/{year}/{issue}",Method="POST")] 

Article AddArticle(string year, string issue, Article article);

我的网址为http://localhost:1355/Issues.svc/

如果我这样做,我将从数据库中获取所有数据

http://localhost:1355/Issues.svc/2010/June/A

GetArticle方法触发过滤后的数据从db。

类似地,我必须调用Add Article(WebInvoke)方法将数据插入数据库。 我该如何在浏览器中调用此方法

我的网址应该是什么,我应该给method = post

3 个答案:

答案 0 :(得分:1)

查看此帖子可帮助您完成所需的任务:Create REST service with WCF and Consume using jQuery

答案 1 :(得分:1)

只需修改网址,您就无法从浏览器发送HTTP帖子。您必须拥有一个包含HTML表单,一些Javascript代码,一些服务器端代码或其他能够向您的服务URL发出HTTP POST请求的网页。

如果您只是想在开发过程中测试您的服务,这里有一个很好的HTTP调试工具,您可能想要查看:http://fiddler2.com

答案 2 :(得分:0)

您无法使用浏览器网址发布。

试试此代码

//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost/DemoApp/Default.aspx") as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = "POST";
//Data to Post to the Page, itis key value pairs; separated by "&"
string data = "Username=username&password=password";
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
    MessageBox.Show(sr.ReadToEnd());
}

来源:http://www.dotnetthoughts.net/2009/11/10/post-data-using-httpwebrequest-in-c-sharp/