1

我编写了一个程序,在网站上搜索用户名并下载他们的主 html 页面,找到到下一页的链接并下载等等。当我第一次运行程序时,搜索的获取响应正常工作,下载所有 html 页面的获取响应正常工作。但是,当我进行第二次搜索时,搜索的获取响应失败。我认为可能是服务器在一段时间内只允许这么多搜索,但事实并非如此。当我关闭我的程序并再次运行它时,一切正常。为什么除了最初的尝试之外,get 响应在任何尝试中都会失败?您不能将 Web 请求定义为新请求,并且一旦获得响应以释放资源,它们似乎无法关闭 Web 请求。是因为我在后台工作人员中运行整个过程吗?我不知道怎么了。就在昨天,我总是可以进行尽可能多的搜索...

这是我的搜索表单代码:

Private Sub FormSearch_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'if the user name is empty then initialize it
    If My.Settings.UserName = "" Then
        My.Settings.UserName = ""
    End If
    'if the current url is empty then initialize it
    If My.Settings.CurrentURL = "" Then
        My.Settings.CurrentURL = ""
    End If
    My.Settings.Save()
End Sub

Private Sub TextBoxUserName_TextChanged(sender As Object, e As EventArgs) Handles TextBoxUserName.TextChanged
    'if the text length of the text box is greater then zero
    If TextBoxUserName.TextLength > 0 Then
        'enable the search button
        ButtonSearch.Enabled = True
    Else
        'disable the search button and set focus on the text box
        ButtonSearch.Enabled = False
        TextBoxUserName.Focus()
    End If
End Sub

Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
    'trim the text box of any spaces
    My.Settings.UserName = Trim(TextBoxUserName.Text)
    'if the user name is not equal to null
    If My.Settings.UserName <> "" Then
        'set the current url and test if it exists
        My.Settings.CurrentURL = "*url here*"
        If URLExists(My.Settings.CurrentURL) = True Then
            'save the settings
            My.Settings.Save()
            'return ok for the dialog result and close the form
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        Else 'cannot find the account
            'set the text box text to null and set focus on it
            TextBoxUserName.Text = ""
            TextBoxUserName.Focus()
        End If
    End If
End Sub

Private Function URLExists(url As String) As Boolean
    'create a new uri
    Dim uriURL As New Uri(url)
    'create a new web request of the uri
    Dim wrRequest As WebRequest = WebRequest.Create(uriURL)
    'set the web request timeout and method
    wrRequest.Timeout = 30000
    wrRequest.Method = "GET"
    'create a new web response
    Dim wrResponse As WebResponse
    Try
        'if the response succeeds then return true
        wrResponse = wrRequest.GetResponse
        Return True
    Catch ex As Exception 'the response failed so return false
        LabelMessage.Text = ex.Message
        Return False
    End Try
End Function
4

0 回答 0