2016-08-17 27 views
0

我確實有一個WCF RESTful .NET 4.0服務,它從URI的主體運行和使用值#Json。WCF REstful Service在Uri上傳遞用戶和密碼

我調用我的方法的方法如下:

利用海報,爲POST請求 http://localhost:25512/JSONService1.svc/CreateItem

身體:

= YOLO

工作正常!

,但我想消費的URI爲好,

http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111

這裏是我的代碼

這是Service1.cs

[OperationContract] 
[WebInvoke(UriTemplate = "CreateItem", // ISSUES <<< not sure what to do here 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, Method = "POST")]  

void CreateItem(Stream streamOfData,string uid, string pass); 

,這與相關的代碼它,Service1.svc.cs

public void CreateItem(Stream streamOfData, string uid, string pass) 
{ 
     StreamReader reader = new StreamReader(streamOfData); 
     String res = reader.ReadToEnd(); 
     NameValueCollection coll = HttpUtility.ParseQueryString(res); 
} 
+0

你是什麼意思「我想消耗uri」? –

+0

也許我應該稱之爲URL而不是URI,我想要傳遞/使用「uid和pass」參數並在CreateItem方法中使用它們 – HoneyDipper

+0

好的,所以你想知道客戶端發送請求時實際的URI是什麼? –

回答

0

要獲得原始的URI,你可以做到以下幾點:

string originalUri = System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString; 

您可以使用System.Uri類和/或HttpUtility類來解析URI。

使用System.Uri類只是通過Uri。

Uri myUri = new Uri("http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111"); 

或在您的要求:

Uri myUri = new Uri(originalUri); 

然後,你可以通過使用一些內置的性能檢查各條URI的。

+0

謝謝= D,幫助 – HoneyDipper

相關問題