如何验证RIA服务URL

时间:2012-12-20 15:47:44

标签: c# .net validation wcf-ria-services

我有这个RIA服务网址:

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

所以我需要在我的应用程序的配置窗口中以某种方式验证此URL。

我使用这种方法来做到这一点:

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;
        }

如果我使用一些不正确的网址(如

),则会出现主要问题
http://192.168.2.100/MegaSystem/Services/RIAServicesLibraryMain-Web-Version_1_0-DomainService_SHIT_SHIT_SHIT.svc

无论如何它都返回TRUE ......

请帮我找到验证RIA服务网址的正确方法。

谢谢!

1 个答案:

答案 0 :(得分: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;
        }