VB.NET检查输入是否是链接

时间:2011-11-30 23:05:47

标签: .net vb.net text-files

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
        'DO NOTHING
    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

到目前为止,这是有效的,除非我输入随机文本。如果输入了在线.txt,我只需要运行它。

我试过System.IO.File.Exists,但那更多本地文件对吗?任何帮助将非常感激。谢谢你们!

1 个答案:

答案 0 :(得分:1)

检测文本框是否包含合法URL(请注意以下行:如果hwresponse.StatusCode = Net.HttpStatusCode.OK,那就是确认其不是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 = ""
  Try
    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)
      postStream.Close()
    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
    hwresponse.Close()
    Catch e As Exception
      responseData = "An error occurred: " & e.Message
    End Try
  Return responseData
End Function