2011-11-17 116 views
1

我已經成功地安裝了以下WCF合同作爲一種服務:調用一個WCF WebInvoke方法與HttpWebRequest的POST和映射參數正確

[WebInvoke(Method = "POST", UriTemplate = "submit/{apiKey}", ResponseFormat = WebMessageFormat.Xml)] 
[OperationContract] 
DataServiceResult Submit(string apiKey, string email); 

我試圖把這個使用的HttpWebRequest

string url = "http://localhost:51462/Api.svc/web/submit/key"; 
string formatString = "[email protected]"; 

byte[] postData = Encoding.ASCII.GetBytes(formatString); 

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
request.Method = "POST"; 
request.Referer = url; 
request.KeepAlive = false; 
request.ContentType = "application/x-www-form-urlencoded"; 
request.ContentLength = postData.Length; 

System.IO.Stream outputStream = request.GetRequestStream(); 
request.AllowAutoRedirect = true; 
outputStream.Write(postData, 0, postData.Length); 
outputStream.Close(); 

HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
Stream responseStream = response.GetResponseStream(); 
StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); 
string srcString = reader.ReadToEnd(); 

當我運行這個代碼時,apikey被成功映射。問題是我不確定如何將電子郵件參數映射到當前爲空的提交方法。

另一個說明,在我的應用程序中,我傳遞了10-15個參數作爲字符串,因此向UriTemplate添加電子郵件不是我正在尋找的答案。

回答

0

只是檢查,爲什麼不傳遞一個對象(XML或JSON)在URL中的一個選項?

你可以看看這個post作爲參考。讓我知道如果我把它全部弄錯了:)