1

我给了

[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

4

3 回答 3

1

仅通过修改 URL,您将无法从浏览器发送 HTTP 帖子。您必须有一个带有 HTML 表单、一些 Javascript 代码、一些服务器端代码或其他能够向您的服务 URL 发出 HTTP POST 请求的网页。

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

于 2011-04-08T04:48:25.867 回答
1

检查这篇文章可以帮助您完成您想要的任务:使用 WCF 创建 REST 服务并使用 jQuery 使用

于 2011-04-08T04:43:54.597 回答
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/

于 2011-04-08T04:51:32.097 回答