Webrequest第二次使用时不在函数中工作

时间:2013-09-26 17:03:11

标签: vb.net webrequest

我已经创建了一个函数来创建webrequest并在我的网站上加载api以连接带参数的ssh。

这是功能代码:

    Public Function sshExec(ByVal command As String)

    Try
        Dim request As WebRequest = _
  WebRequest.Create("http://api.***.be/ssh.php?host=" + ComboBox1.Text + "&username=root&password=" + TextBox1.Text + "&command=" + command)
        ' Get the response.
        Dim response As WebResponse = request.GetResponse()
        ' Get the stream containing content returned by the server.
        Dim dataStream As Stream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()

        If responseFromServer.Contains("Login Failed") Then
            lblStatus.Text = "Login failed"
            lblStatus.ForeColor = Color.Red
        Else
            lblStatus.ForeColor = Color.Green
            lblStatus.Text = "Connected"
        End If

        TextBox2.Text = "http://api.***.be/ssh.php?host=" + ComboBox1.Text + "&username=root&password=" + TextBox1.Text + "&command=" + command

        ' Clean up the streams and the response.
        reader.Close()
        response.Close()

        Return responseFromServer

    Catch ex As Exception


    End Try

End Function

我创建了一个文本框,当我使用该函数时,它会将它加载的api链接放在文本框中。如果我复制它并将其加载到我的浏览器中,它可以正常工作但是通过webrequest它会说:

Notice:  Cannot connect to 62.113.*** Error 0. php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/niel/public_html/api/Net/SSH2.php on line 831
Login Failed

此外,当我第二次使用该功能时,它根本不加载页面。

任何人都知道什么是错的?在此先感谢:)

1 个答案:

答案 0 :(得分:0)

很可能是因为你没有处置response,所以只需调用

response.Dispose()

关闭后。