正确获取Web响应状态码

Get Web Response Status Code properly

我正在使用 Imgur 的 API 匿名上传图像文件。

问题出在下面的函数上,我正在尝试检索和解析 Select Case 上的响应代码,但如果出现问题(任何非 200 状态代码),则指令 Dim response As Byte() = wc.UploadValues(...) 抛出异常,所以 select 的情况都被遗漏了,或者说,上传失败时我无法获取到状态码,因为 UploadValues 方法抛出异常。

我该如何解决这个问题?

这是我正在使用的代码:

    Public Function UploadImage(ByVal img As String) As ImgurImage

        Try

            ' Create a WebClient. 
            Using wc As New WebClient()

                ' Read the image.
                Dim values As New NameValueCollection() From
                    {
                        {"image", Convert.ToBase64String(File.ReadAllBytes(img))}
                    }

                ' Set the Headers.
                Dim headers As New NameValueCollection() From
                    {
                        {"Authorization", String.Format("Client-ID {0}", Me.ClientId)}
                    }

                ' Add the headers.
                wc.Headers.Add(headers)

                ' Upload the image, and get the response.
                Dim response As Byte() = wc.UploadValues("https://api.imgur.com/3/upload.xml", values)

                ' Read the response (Converting Byte-Array to Stream).
                Using sr As New StreamReader(New MemoryStream(response))

                    Dim serverResponse As String = sr.ReadToEnd
                    Dim xdoc As New XDocument(XDocument.Parse(serverResponse))
                    Dim status As ImgurStatus = Nothing

                    status = Me.GetResultFromStatus(Convert.ToInt32(xdoc.Root.LastAttribute.Value.ToString))

                    Select Case status

                        Case ImgurStatus.Success
                            Return New ImgurImage(New Uri(xdoc.Descendants("link").Value))

                        Case ImgurStatus.AccessForbidden
                            RaiseEvent OnAccessForbidden(Me, ImgurStatus.AccessForbidden)

                        Case ImgurStatus.AuthorizationFailed
                            RaiseEvent OnAuthorizationFailed(Me, ImgurStatus.AuthorizationFailed)

                        Case ImgurStatus.BadImageFormat
                            RaiseEvent OnBadImageFormat(Me, ImgurStatus.BadImageFormat)

                        Case ImgurStatus.InternalServerError
                            RaiseEvent OnInternalServerError(Me, ImgurStatus.InternalServerError)

                        Case ImgurStatus.PageIsNotFound
                            RaiseEvent OnPageIsNotFound(Me, ImgurStatus.PageIsNotFound)

                        Case ImgurStatus.UploadRateLimitError
                            RaiseEvent OnUploadRateLimitError(Me, ImgurStatus.UploadRateLimitError)

                        Case ImgurStatus.UnknownError
                            RaiseEvent OnUnknownError(Me, ImgurStatus.UnknownError)

                    End Select

                End Using '/ sr As New StreamReader

            End Using '/  wc As New WebClient()

        Catch ex As Exception
            RaiseEvent OnUnknownError(Me, ImgurStatus.UnknownError)

        End Try

        Return Nothing

    End Function

首先捕获网络异常,然后检查结果以了解真正的问题所在。

Dim response As Byte()

Try
    response = wc.UploadValues("https://api.imgur.com/3/upload.xml", values)
Catch we As WebException
    ' determine web exception from Response.GetResponseStream

    Dim resp As String
    resp = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
    ' imgUr's WebEx for Pins returns JSOn, so assume that here,
    ' but you wont know until you look at it:
    SvrResponses = CType(json.DeserializeObject(resp.ToString), 
                                    Dictionary(Of String, Object))

    ' todo examine SvrResponses to figure out the problem
Catch ex As Exception
    ' other problem
End Try