如何在C#winform应用程序中验证URL [英] How to validate a url in C# winform application
本文介绍了如何在C#winform应用程序中验证URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C#中验证网址。
提前致谢
Hi,
How to validate a url in C#.
Thanks in advance
推荐答案
使用Uri.IsWellFormedUriString
检查网址格式是否正确。
例如,
UseUri.IsWellFormedUriString
to check whether the URL is in correct form.
For example,
Uri.IsWellFormedUriString("http://www.google.com", UriKind.Absolute);
It当URL格式正确时,将返回true。
要检查URL是否存在,请创建 WebRequest
:
It'll return true as the URL is in correct form.
To check the existence of a URL create a WebRequest
:
WebRequest request = WebRequest.Create("http://www.google.com");
try
{
request.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
/// The URL is incorrect
MessageBox.Show("The URL is incorrect");
}
使用Uri.TryCreate方法验证网址。
请参考以下链接
http://stackoverflow.com/questions/3228984/a-better-way-to-validate-url -in-c-sharp-than-try-catch [ ^ ]
MSDN链接:
http://msdn.microsoft.com /en-us/library/system.uri.trycreate.aspx [ ^ ]
这篇关于如何在C#winform应用程序中验证URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文