HttpWebRequest上的404错误

时间:2016-11-09 16:21:53

标签: vb.net vb.net-2010

我试图检查特定网站上的用户名是否可用,但是它出现了404错误。

Dim Request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://www.instagram.com/" + userToCheck & "/")
        Dim response As System.Net.HttpWebResponse = Request.GetResponse()

        Dim sReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream)
        Dim steamResult As String = sReader.ReadToEnd()

        If steamResult.Contains("Sorry") Then
            addAvailable()

虽然这有效:

 Dim Request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://steamcommunity.com/id/" + userToCheck)
            Dim response As System.Net.HttpWebResponse = Request.GetResponse()

            Dim sReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream)
            Dim steamResult As String = sReader.ReadToEnd()

            If steamResult.Contains("The specified profile could not be found.") Then
                addAvailable()
            End If

任何想法有什么不对?

1 个答案:

答案 0 :(得分:0)

如果通过“停止”表示它正在抛出异常,则必须将代码包装在try catch中以便继续执行。

Try
        Dim Request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://www.instagram.com/" + userToCheck & "/")
        Dim response As System.Net.HttpWebResponse = Request.GetResponse()

        Dim sReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream)
        Dim steamResult As String = sReader.ReadToEnd()

        If steamResult.Contains("Sorry") Then
            addAvailable()
    Catch ex As System.Net.WebException
        Console.WriteLine(ex.Message)
    End Try

编辑:在catch块的404中做任何你想做的事情:)