Check if URL has valid page - vb.net

I want to check if URL has valid page (not 404, just 200).
Code I've tried:
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(myurl), HttpWebRequest)
request.KeepAlive = True
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.OK Then
MsgBox("OK")
End If
Yet every URL that I enter leaves OK response, even if I enter http://mywebsite.com/blahblah.
It's not same on all websites (works fine with example.com), but it doesn't work on my website. Why?
In my browser I see 404 page, but code says it's OK.
Edit: Just to mention that my website has Cloudflare on.

Try this out and see if it work's for you... As mentioned above in my comment's:
A 404 just means the page isn't found on the server, status will return ok even if a page isn't found and the server was reached and responded
Public Class WebPage
Public Property PageSource As String = String.Empty
Public Property Status As HttpStatusCode = HttpStatusCode.NotFound
Public Property WebError As String = String.Empty
End Class
Public Shared Function GetWebPage(ByVal Website As String) As WebPage
Dim web As New WebPage() With {.Status = HttpStatusCode.OK}
Try
Using source As New System.Net.WebClient()
web.PageSource = source.DownloadString(Website)
End Using
Return web
Catch exweb As WebException
If exweb.Status = WebExceptionStatus.ProtocolError AndAlso exweb.Message.Contains("404") Then
web.Status = HttpStatusCode.NotFound
Else
web.Status = HttpStatusCode.BadRequest
End If
web.WebError = exweb.Message
Catch ex As Exception
web.Status = HttpStatusCode.NotFound
web.WebError = ex.Message
End Try
Return web
End Function
Usage Example
Dim webObj As WebPage = GetWebPage("THESITE")
If Not String.IsNullOrEmpty(webObj.WebError) Then
MessageBox.Show(webObj.WebError)
ElseIf webObj.Status = HttpStatusCode.OK Then
MessageBox.Show("OK")
End If

This will do what you want.
Function URLExists(url As String) As Boolean
Dim Request As Object
Dim ff As Integer
Dim rc As Variant
On Error GoTo EndNow
Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
With Request
.Open "GET", url, False
.send
rc = .StatusText
End With
Set Request = Nothing
If rc = "OK" Then URLExists = True
Exit Function
EndNow:
End Function

Related

Getting "The underlying connection was closed: An unexpected error occurred on a receive." on FTPWebResponse.close()

I have a VB.Net function that will check if the FTP directory exists, otherwise create the directory.
The function works fine at first, but when the number of files in the FTP directory has reached a certain limit, response.close() in the finally block throws an exception : System.Net.WebException:The underlying connection was closed: An unexpected error occurred on a receive..
my function
Public Function funFTPCheckFolder(ByVal ftpPath As String, Optional ByVal bCreate As Boolean = False) As Boolean
Dim request As FtpWebRequest = Nothing
Dim response As FtpWebResponse = Nothing
Dim strReader As StreamReader = Nothing
Try
request = WebRequest.Create(ftpPath)
With request
.Credentials = New NetworkCredential(_username, _password)
.Method = WebRequestMethods.Ftp.ListDirectory
.KeepAlive = False
'.UsePassive = True
End With
response = CType(request.GetResponse, FtpWebResponse)
Catch ex As Exception
funFTPCheckFolder = False
If (bCreate = True) Then
request = WebRequest.Create(ftpPath)
With request
.Credentials = New NetworkCredential(_username, _password)
.Method = WebRequestMethods.Ftp.MakeDirectory
' .KeepAlive = False
.UsePassive = True
End With
response = CType(request.GetResponse, FtpWebResponse)
funFTPCheckFolder = True
End If
Exit Function
Finally
If response IsNot Nothing Then
response.Close()
response = Nothing
End If
If request IsNot Nothing Then
request.Abort()
End If
End Try
Return True
End Function
In this case, when the file amount in the FTP directory reached 481, an exception is thrown.
exception message
After moving one file to the subdirectory, it works fine again, so it seems to be a problem with the amount of file in the directory.
For now the only way I can come up with is wrap it with try/catch, but not sure will it cause any other error.
Try
If response IsNot Nothing Then
response.Close()
End If
Catch ex As Exception
End Try
I want to find out what exactly cause the problem, any thoughts would be appreciated!

How to check if website is up?

I'm learning about vb.net and was wondering how I would check for a website, if it is up, set a public shared, if it is down, use a different public shared?
I currently have the code as
Public Shared domain As String = "domain"
But I'm confused as how to make it check the site when I launch the application and then run the check.
--- Edit this is what I have now,
Dim URL As String = "https://www.personalblog.com"
Public Shared Function IsWebpageOnline(ByVal URL As String) As Boolean
Try
Dim req As WebRequest = WebRequest.Create(URL)
Dim res As WebResponse = req.GetResponse()
Catch ex As Exception
Return False
End Try
Return True
End Function
If IsWebpageOnline = 1 Then
Public Shared domain As String = "blog.com"
Else
Public Shared domain As String = "backupblog.com"
End If
You can use this function to check a webpage is online or not.
Public Shared Function IsWebpageOnline(ByVal URL As String) As Boolean
Try
Dim req As WebRequest = WebRequest.Create(URL)
Dim res As WebResponse = req.GetResponse()
Catch ex As Exception
Console.WriteLine(ex.Message)
Return False
End Try
Return True
End Function
It will try to make a get request to web server and then return a bool value
But I must suggest, switch to C# while its early.

WebRequest and WebResponse loading very slow vb.net

I have around 40 values in my database. I am checking every single url if it is online or offline. This is the function I am using:
Public Function CheckAddress(ByVal URL As String) As Boolean
Try
Dim request As WebRequest = WebRequest.Create(URL)
Dim response As WebResponse = request.GetResponse()
Catch ex As Exception
Return False
End Try
Return True
End Function
But this is taking too much time. How can we load the page faster?
I added timeout to WebRequest. Like:
Dim request As WebRequest = WebRequest.Create(URL)
request.Timeout = 50
Maybe you can check for headers only, via the HEAD method:
Public Function CheckAddress(ByVal URL As String) As Boolean
Try
Dim request As WebRequest = WebRequest.Create(URL)
request.Method = "HEAD" ' only headers!
Dim response As WebResponse = request.GetResponse()
If response IsNot Nothing AndAlso _
response.Headers IsNot Nothing AndAlso _
response.Headers.count > 0
...
' check if you get anything
Return True
else
Return false
end if
Catch ex As Exception
Return False
End Try
End Function
This may be quicker.

Dropnet code in vb not working

I'm trying to use dropnet for file upload on Dropbox in vb, but does not work. Results the following error: Received Response [Unauthorized]: Expected to see [OK]. The HTTP response was [{"error": "Request token not found."}]
Here is my code:
_client = New DropNetClient("xxxxxxx", "xxxxxxx")
Dim login As UserLogin = _client.GetToken()
_client.UserLogin = login
Dim url = _client.BuildAuthorizeUrl()
Process.Start(url)
Dim tokenAcess = _client.GetAccessToken()
_client.GetAccessTokenAsync(Function(accessToken)
_client = New DropNetClient("xxxxxx", "xxxxxx", tokenAcess)
End Function, Function([error]) MessageBox.Show([error].Message)
End Function)
Try
Dim rawData As Byte() = File.ReadAllBytes("c:\image.png")
Dim result As MetaData = _client.UploadFile("/", "image.png", rawData)
MessageBox.Show("Successfully uploaded to Dropbox.", "Uploaded to Dropbox")
Catch dropboxEx As Exception
MessageBox.Show("Error: " + dropboxEx.Message)
End Try

VB.NET - Checking FTP directory exists always returns true

Can anybody tell me why the function below always returns true, even if the FTP directory in question does not exist?
The value of directoryURL which I pass in is of the form:
ftp://ip_address/directory/subdirectory/
and has a trailing forward slash.
Public Function DoesDirectoryExist(directoryUrl As String) As Boolean
' Check that the target URL is properly formatted
If Not directoryUrl.StartsWith("ftp://") Then directoryUrl = "ftp://" & directoryUrl
' Create a web request
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(directoryUrl), FtpWebRequest)
request.Credentials = New NetworkCredential(_userName, _password)
request.Method = WebRequestMethods.Ftp.ListDirectory
' Try and list the contents of the directory
Try
Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
' We have been succesful so the directory exists
Return True
End Using
Catch ex As WebException
Dim response As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
Return False
Else
Throw New ApplicationException("Unable to determine if FTP directory exists.")
End If
End Try
End Function
Weird. This works for me (I don't cast the request but I guess that shouldn't matter). This is the code I usually rely on:
Dim response As FtpWebResponse = request.GetResponse()
Using (response)
found = True
End Using
The alternative you have is reading the list of directories:
Using sr As New System.IO.StreamReader(response.GetResponseStream())
Using sw As New System.IO.StreamWriter("tempfile", False)
sw.Write(sr.ReadToEnd())
End Using
End Using
In the worst scenario, it should help you to tackle the problem (e.g., it always founds a directory called "ghost", which you might use to trigger the not-found).
Method 1
Public Function DirectoryExists(directory As String) As Boolean
Dim directoryExists__1 As Boolean
Dim request = DirectCast(WebRequest.Create(directory), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.ListDirectory
request.Credentials = New NetworkCredential("user", "pass")
Try
Using request.GetResponse()
directoryExists__1 = True
End Using
Catch generatedExceptionName As WebException
directoryExists__1 = False
End Try
Return directoryExists__1
End Function
Method 2
If Not DirectoryExists("ftp://" + FTPSettings.IP + "/" + lo_ScreenShotPath) Then
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + lo_ScreenShotPath)), FtpWebRequest)
End If
i hope may i help this...