0

我有这个 RIA 服务 URL:

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

如果我使用一些不正确的 URL,例如

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

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

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

谢谢!

4

1 回答 1

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;
        }
于 2012-12-20T16:06:50.313 回答