假设我有一些用户输入一些WCF / RIA服务URL的表单,他想保存它并确保该URL能够正常工作。
(主要条件是它可以是任何WCF / RIA服务URL。)
如何正确解析以确保此WCF / RIA服务网址有效?
哪种策略最好?
谢谢!
答案 0 :(得分:2)
使用以下代码获取状态响应以确保URL有效:
var request = WebRequest.Create(serviceUrl) as HttpWebRequest;
if (request != null)
{
request.ContentType = "application/xml";
request.Method = "GET";
}
var response = request.GetResponse() as HttpWebResponse;
if(response.StatusCode == HttpStatusCode.OK)
{
//valid url, continue to persist.
}
else
{
//invalid url throw your exception or show message.
}