Search code examples
c#.netvalidationwcf-ria-services

How to validate RIA Services URL


I have this RIA Service URL:

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

So I need to validate this URL somehow in the configuration window of the my application.

I use this method to do it:

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

The main problem if I use some incorrect URL like

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

It returns TRUE anyway...

Please help me to find correct way to validate RIA Services URL.

Thank you!


Solution

  • I found good solution:

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