2012-04-11 42 views
0

我使用Http WebRequest調用我的WCF WebService。它的工作很好,但是當我在參數中傳遞一些特殊字符時,它會導致錯誤404.找不到頁面。使用WebRequest獲取wcf服務getmethod給404錯誤

這是我的代碼片段。

[OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      UriTemplate = "Data/{username}/{password}")] 
     string RequestData(string userName, string password); 
    public string RequestData(string userName, string password) 
     { 
      // Here is My Logic Comes 

     } 

// Here im Calling This Service in My ASPX Page 

HttpWebRequest req = null; 
      HttpWebResponse res = null; 
      try 
      { 
       const string url = "http://localhost:53366/AuthService.svc/Data/[email protected]/password#1"; // Here Gives Error 404 Page Not Found 

//const string url = "http://localhost:53366/AuthService.svc/Data/test/password"; // Here Works Fine When I Pass This Paramters Without Special Characters 
       req = (HttpWebRequest)WebRequest.Create(url); 
       req.Method = "Get"; 
       req.ContentType = "application/json; charset=utf-8"; 

       req.Headers.Add("AppID", "MobileApplication"); 

       res = (HttpWebResponse)req.GetResponse(); 
       Stream responseStream = res.GetResponseStream(); 
       var streamReader = new StreamReader(responseStream); 

       txtTokenRespoonse.Text = streamReader.ReadToEnd(); 

       streamReader.Close(); 
       streamReader.Dispose(); 

       responseStream.Close(); 
       responseStream.Dispose(); 
      } 

回答

0

如果要將任何特殊字符傳遞給URL,則必須對其進行編碼!

嘗試:

const string url = "http://localhost:53366/AuthService.svc/Data/test%40test.com/password%231"; 

順便說一句。在URL中傳遞密碼只是一個示例,不是嗎?