第二次尝试时,Web请求获取响应失败

时间:2013-07-09 14:49:56

标签: vb.net httpwebrequest getresponse

我写了一个程序,在网站上搜索用户名并下载他们的主html页面,找到下一页的链接并下载等等。当我第一次运行程序时,搜索的get响应正常,并且下载所有html页面的get响应正常工作。但是,当我进行第二次搜索时,搜索的get响应失败。我想也许是服务器只允许在一段时间内进行如此多的搜索,但事实并非如此。当我关闭我的程序并再次运行它一切正常。除了最初的尝试之外,为什么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

0 个答案:

没有答案