2011-04-08 109 views
1

我已经给WCF服务呼叫期间后

[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)方法将数据插入到数据库中。 我应该如何调用该方法在浏览器

怎么我的网址应该是我应该给方法=张贴

+0

你不能这样做仅在浏览器后 - 你需要一些附加工具,如[小提琴手(HTTP:// WWW .fiddler2.com/fiddler2 /) – 2011-04-08 04:48:13

回答

1

你不会只需修改URL即可从浏览器发送HTTP信息。您必须拥有一个包含HTML表单,一些Javascript代码,一些服务器端代码或其他能够向您的服务URL发出HTTP POST请求的网页。

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

0

不能使用浏览器的URL后使用它。

试试这个代码

//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/