忽略响应错误VB.NET

忽略响应错误VB.NET,vb.net,post,request,httpwebrequest,Vb.net,Post,Request,Httpwebrequest,我在获取响应时遇到了一个问题,当我得到像400错误或403错误这样的响应时,会弹出意外的事件错误。 我的问题是如何忽略这些错误 代码如下: Dim postData As String = "{""agent"": {""name"": ""Minecraft"",""version"": 1},""username"": " + TextBox1.Text + ",""password"": " + TextBox2.Text + ",""clientToken"": ""cl

我在获取响应时遇到了一个问题,当我得到像400错误或403错误这样的响应时,会弹出意外的事件错误。 我的问题是如何忽略这些错误

代码如下:

        Dim postData As String = "{""agent"": {""name"": ""Minecraft"",""version"": 1},""username"": " + TextBox1.Text + ",""password"": " + TextBox2.Text + ",""clientToken"": ""client identifier""}"
    Dim tempCookies As New CookieContainer
    Dim encoding As New UTF8Encoding
    Dim byteData As Byte() = encoding.GetBytes(postData)

    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://authserver.mojang.com/authenticate"), HttpWebRequest)
    postReq.Method = "POST"
    postReq.KeepAlive = True
    postReq.CookieContainer = tempCookies
    postReq.ContentType = "application/json"
    postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
    postReq.ContentLength = byteData.Length
    postReq.UseDefaultCredentials = True
    postReq.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
    postReq.Credentials = CredentialCache.DefaultCredentials
    Dim postreqstream As Stream = postReq.GetRequestStream()
    postreqstream.Write(byteData, 0, byteData.Length)
    postreqstream.Close()
    Dim postresponse As HttpWebResponse


    postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
    tempCookies.Add(postresponse.Cookies)
    logincookie = tempCookies
    Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
    Dim thepage As String = postreqreader.ReadToEnd


    RichTextBox1.Text = thepage
    If thepage.Contains(":true") Then
        MsgBox("Valid")
    End If
    If thepage.Contains("ForbiddenOperationException") Then
        MsgBox("Not Valid")
    End If

在要忽略异常的每个语句周围,将try/catch放在“catch”为空的位置:

Try
    ... single statement
Catch
   //empty
End Try

但是,您确定要忽略异常吗?

忽略错误的两个选项都很糟糕,但您应该能够通过结构化异常处理更好地做到这一点。我如何通过结构化异常处理做到这一点?如果您只想忽略,请改为使用“错误恢复下一步”“Try/Catch”用于每个要忽略异常的语句,并且“Catch”为空。我假设错误91上的无限循环是无意的?
Try
    ... single statement
Catch
   //empty
End Try