2012-12-20 67 views
0

我有這樣的RIA服務網址:如何驗證RIA服務URL

http://192.168.2.100/MegaSystem/Services/RIAServicesLibraryMain-Web-Version_1_0-DomainService.svc 

所以我需要在我的應用程序的配置窗口某種程度上驗證了該網址。

我用這個方法來做到這一點:

private bool KickServices(string serviceUrl) 
     { 
      bool result = false; 

      var request = WebRequest.Create(serviceUrl) as HttpWebRequest; 
      if (request != null) 
      { 
       request.ContentType = "application/xml"; 
       request.Method = "GET"; 
      } 

      if (request != null) 
      { 
       var response = request.GetResponse() as HttpWebResponse; 
       if (response != null && response.StatusCode == HttpStatusCode.OK) 
       { 
        result = true; 
       } 
      } 

      return result; 
     } 

主要的問題,如果我使用了一些不正確的URL像

http://192.168.2.100/MegaSystem/Services/RIAServicesLibraryMain-Web-Version_1_0-DomainService_SHIT_SHIT_SHIT.svc 

返回true反正...

請幫我找到正確的方法來驗證RIA服務URL。

謝謝!

回答

0

我找到很好的解決辦法:

private bool KickServices(string serviceUrl) 
     { 
      bool result = false; 

      var request = WebRequest.Create(serviceUrl) as HttpWebRequest; 
      if (request != null) 
      { 
       request.ContentType = "application/xml"; 
       request.Method = "GET"; 
      } 

      if (request != null) 
      { 
       var response = request.GetResponse() as HttpWebResponse; 
       if (response != null && response.StatusCode == HttpStatusCode.OK) 
       { 
        string resultContent = null; 
        Encoding responseEncoding = Encoding.GetEncoding(response.CharacterSet); 
        using (var sr = new StreamReader(response.GetResponseStream(), responseEncoding)) 
        { 
         resultContent = sr.ReadToEnd(); 
         if (resultContent.Contains(serviceUrl)) 
          result = true; 
        } 
       } 
      } 

      return result; 
     }