Rick Bross Rick Bross - 5 months ago 33
Vb.net Question

VB.NET check if input is a link

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If txtSubd.Text = "" Then 'If the input is blank
Else ' If its not, proceed

Dim wb As New WebClient

If wb.DownloadString(txtSubd.Text) = "" Then 'Check if the file entered is blank

MsgBox("Text file is Blank.", MsgBoxStyle.Exclamation, "Error")

Else 'If not, proceed

My.Forms.frmMain.TXTFILE.Text = txtSubd.Text

End If
End If

End Sub

So far this works, unless I enter random text. I need this only to run if a online .txt is entered.

I tried System.IO.File.Exists, but thats more local files right? ANy help would be much appreciated. Thanks guys!


Detect if the textbox contains a legit URL (notice the line: If hwresponse.StatusCode = Net.HttpStatusCode.OK, thats the ticket to confirm its not a 404) :

Dim wData as String = WRequest("http://server/sfasnasjnksa", "GET", "")

Function WRequest(URL As String, method As String, POSTdata As String) As String
  Dim responseData As String = ""
    Dim hwrequest As Net.HttpWebRequest = Net.Webrequest.Create(URL)
    hwrequest.Accept = "*/*"
    hwrequest.AllowAutoRedirect = true
    hwrequest.UserAgent = "http_requester/0.1"
    hwrequest.Timeout = 60000
    hwrequest.Method = method
    If hwrequest.Method = "POST" Then
      hwrequest.ContentType = "application/x-www-form-urlencoded"
      Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
      Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
      hwrequest.ContentLength = postByteArray.Length
      Dim postStream As IO.Stream = hwrequest.GetRequestStream()
      postStream.Write(postByteArray, 0, postByteArray.Length)
    End If
    Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
    If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
      Dim responseStream As IO.StreamReader = _
        New IO.StreamReader(hwresponse.GetResponseStream())
      responseData = responseStream.ReadToEnd()
    End If
    Catch e As Exception
      responseData = "An error occurred: " & e.Message
    End Try
  Return responseData
End Function