验证
URL的函数,定义了一个Uri类存储要验证的URL,利用GetResponse返回对Internet请求的响应, 转化为HttpWebResponse类得到获取响应的状态StatusCode.

 

[Visual Basic]


如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl        Dim NewUri As New Uri("Http://" + url)
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl        
Try
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl            
Dim httpReq As System.Net.WebRequest = System.Net.WebRequest.Create(NewUri)
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl            
Dim httpRes As System.Net.WebResponse = httpReq.GetResponse()
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl            
Dim myRes As System.Net.HttpWebResponse = CType(httpRes, System.Net.HttpWebResponse)
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl            
If (myRes.StatusCode = System.Net.HttpStatusCode.OK) Then
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl                
Return True
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl            
Else
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl                
Return False
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl            
End If
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl        
Catch ex As System.Net.WebException
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl            
Return False
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl        
End Try
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl    
End Function
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl

[C#]

如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrlpublic bool function ValidateUrl(string url)
{
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl   Uri newUri 
= new Uri(“Http://“+url);
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl
   try
{
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl     WebRequest httpReq 
= WebRequest.Create(newUri);
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl     WebResponse httpRes 
= httpReq.GetResponse();
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl     HttpWebResponse myRes 
= (HttpWebResponse)httpRes;
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl      
if(myRes.StatusCode==“OK“)
{
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl         
return true;
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl     }

如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl     
else
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl        
return false;
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl   }

如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl   
catch (WebException ex)
{
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl       
return false;
如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl    }

如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl}

如何实现对URL有效性的验证?URL有效性验证函数:ValidateUrl

相关文章: