2011-04-08 110 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/