Windows phone 8 如何检查URL是否有效?
我希望能够检查指定的URL是否有效[最好是活动的和格式良好的] 乙二醇 可能吗?我在stackoverflow中使用了其他帖子上指定的代码,但它们似乎都不起作用。这是一个例子Windows phone 8 如何检查URL是否有效?,windows-phone-8,Windows Phone 8,我希望能够检查指定的URL是否有效[最好是活动的和格式良好的] 乙二醇 可能吗?我在stackoverflow中使用了其他帖子上指定的代码,但它们似乎都不起作用。这是一个例子 private bool checkWebsite(string url){ WebRequest webRequest = WebRequest.Create(url); WebResponse webResponse; try{ //System.Net.WebRequest d
private bool checkWebsite(string url){
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse;
try{
//System.Net.WebRequest does not contain a definition for GetResponse();
webResponse = webRequest.GetResponse();
}
catch
return false;
return true;
}
感谢您的帮助。提前谢谢你。这应该适合你。虽然不是最优雅的代码
static void Main(string[] args)
{
var uriName = "http://www.google.com";
Uri uriResult;
bool isUriValid = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
if (isUriValid)
{
var webRequest = HttpWebRequest.Create(uriResult);
// Only request the headers, not the whole content
webRequest.Method = "HEAD";
try
{
// Exception is thrown if 404
var response = webRequest.GetResponse() as HttpWebResponse;
// Check if status return is 200 OK, then active. You might want to check the HttpStatusCode for others
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine(((int)response.StatusCode).ToString());
}
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
else
{
Console.WriteLine("Url is not valid");
}
Console.Read();
}
这应该对你有用。虽然不是最优雅的代码
static void Main(string[] args)
{
var uriName = "http://www.google.com";
Uri uriResult;
bool isUriValid = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
if (isUriValid)
{
var webRequest = HttpWebRequest.Create(uriResult);
// Only request the headers, not the whole content
webRequest.Method = "HEAD";
try
{
// Exception is thrown if 404
var response = webRequest.GetResponse() as HttpWebResponse;
// Check if status return is 200 OK, then active. You might want to check the HttpStatusCode for others
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine(((int)response.StatusCode).ToString());
}
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
else
{
Console.WriteLine("Url is not valid");
}
Console.Read();
}
使用HttpClient回答(您应该使用System.Net.Http;添加):
专用异步任务iURL可用(Uri url)
{
var httpClient=新的httpClient();
HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Head,url);
HttpResponseMessage response=等待httpClient.SendAsync(请求);
尝试
{
if(response.StatusCode==HttpStatusCode.OK)
返回true;
}
捕获(WebE例外)
{
Debug.WriteLine(e.Message);
}
返回false;
}
使用HttpClient回答(您应该使用System.Net.Http;添加):
专用异步任务iURL可用(Uri url)
{
var httpClient=新的httpClient();
HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Head,url);
HttpResponseMessage response=等待httpClient.SendAsync(请求);
尝试
{
if(response.StatusCode==HttpStatusCode.OK)
返回true;
}
捕获(WebE例外)
{
Debug.WriteLine(e.Message);
}
返回false;
}
您需要更具体地说明“有效”的含义。仅仅是形式良好?或者主动不返回HTTP错误?为什么你认为google.co是无效的?!这是100%有效的网址@如果可能,Cairnarvon结构良好且活跃。我已经更新了这个问题。谢谢@MarcinJuraszek我不知道发生了什么,但是当我刚刚访问链接时,我得到了一个错误503,现在我转向了。我刚刚更新了一个无效链接。谢谢@Jieqin最好的方法可能是使用Regex检查它是否是有效的格式,这样您就不必向服务器发出请求。如果是有效格式,服务器将向服务器发送一个请求(只需请求head并检查响应是否不是404)。我回家后会设法拿到密码。同时,你可以在这里查看我的示例,你需要修改它,这样你就可以抓取第一行-类似这样的HTTP/1.1 404 Not Found
。仅仅是形式良好?或者主动不返回HTTP错误?为什么你认为google.co是无效的?!这是100%有效的网址@如果可能,Cairnarvon结构良好且活跃。我已经更新了这个问题。谢谢@MarcinJuraszek我不知道发生了什么,但是当我刚刚访问链接时,我得到了一个错误503,现在我转向了。我刚刚更新了一个无效链接。谢谢@Jieqin最好的方法可能是使用Regex检查它是否是有效的格式,这样您就不必向服务器发出请求。如果是有效格式,服务器将向服务器发送一个请求(只需请求head并检查响应是否不是404)。我回家后会设法拿到密码。同时,您可以在这里查看我的示例,您需要对其进行修改,以便抓取第一行—类似于HTTP/1.1 404 Not Found的内容我尝试了您的解决方案,但变量“webRequest”没有方法“GetResponse()”。无论如何,谢谢。好的,我明白了。也许WP8不支持它。我下班后几个小时给你回电话。但与此同时,您可以检查“HttpClient”类或WebClient
HttpClient
是微软最新推出的lib。但实现这一点的想法保持不变。发送请求以获取标头并检查响应代码。我尝试了您的解决方案,但变量“webRequest”没有方法“GetResponse()”。无论如何,谢谢。好的,我明白了。也许WP8不支持它。我下班后几个小时给你回电话。但与此同时,您可以检查“HttpClient”类或WebClient
HttpClient
是微软最新推出的lib。但实现这一点的想法保持不变。发送请求以获取标头并检查响应代码。
private async Task<bool> IsURLAvailable(Uri url)
{
var httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, url);
HttpResponseMessage response = await httpClient.SendAsync(request);
try
{
if (response.StatusCode == HttpStatusCode.OK)
return true;
}
catch (WebException e)
{
Debug.WriteLine(e.Message);
}
return false;
}