VB.NET ~ PUT WebRequest 在尝试 GetResponse 时引发异常

发布于 2025-01-13 01:31:12 字数 1544 浏览 0 评论 0原文

我遇到两个例外。第一个在常规代码中,第二个在异常处理块中。

我有这个函数(如下),应该使用 PUT 方法更改 Web API 中的用户设置。一切都运行良好,直到我尝试从网络请求获取响应。然后它引发第一个异常(未知),当我尝试处理错误时,我得到第二个异常“对象引用未设置为对象的实例”。

据我所知,通常会导致此错误的是尝试获取未分配的 Web 请求的响应,但在这种情况下,它已分配且 Uri 有效。所有授权凭据也都是正确的。

这是我的代码:

Try
    Dim webRequest As System.Net.HttpWebRequest
    webRequest = System.Net.HttpWebRequest.Create(InternalSettings.APIurl + "/config")
    webRequest.Headers("Authorization") = InternalSettings.APIpwd
    webRequest.Headers("API-Application-Key") = InternalSettings.APIkey

    webRequest.Method = "PUT"
    webRequest.ContentType = "application/x-www-form-urlencoded"

    Dim postData As String = ""
    postData += "SaveAllCustomersData=false"

    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

    Dim dataStream As Stream = webRequest.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    'The next line will raise an unknown exception
    Dim myHttpWebResponse As HttpWebResponse = webRequest.GetResponse
    Dim responseReader As StreamReader = New StreamReader(myHttpWebResponse.GetResponseStream())
    Dim responseData As String = responseReader.ReadToEnd()

    responseReader.Close()
    webRequest.GetResponse().Close()

Catch ex As WebException
    'The next line will raise a "Object reference not set to an instance of an object" exception
    Dim resp = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
    Dim obj = JsonConvert.DeserializeObject(resp)
    Return False
End Try

I am getting TWO exceptions. The first one in the regular code and the second into the exception handling block.

I have this function (below) that supposed to change user settings in a Web API using PUT method. Everything runs fine until the point that I try to get a response from the web request. Then it raises the first exception (unknown) and when I try to handle the error I get a second exception "Object reference not set to an instance of an object".

As far as I know normally what will cause this error is the attempt of getting a response of an unassigned web request, but in this case it IS assigned and the Uri is valid. All the credentials for authorization are correct too.

This is my code:

Try
    Dim webRequest As System.Net.HttpWebRequest
    webRequest = System.Net.HttpWebRequest.Create(InternalSettings.APIurl + "/config")
    webRequest.Headers("Authorization") = InternalSettings.APIpwd
    webRequest.Headers("API-Application-Key") = InternalSettings.APIkey

    webRequest.Method = "PUT"
    webRequest.ContentType = "application/x-www-form-urlencoded"

    Dim postData As String = ""
    postData += "SaveAllCustomersData=false"

    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

    Dim dataStream As Stream = webRequest.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    'The next line will raise an unknown exception
    Dim myHttpWebResponse As HttpWebResponse = webRequest.GetResponse
    Dim responseReader As StreamReader = New StreamReader(myHttpWebResponse.GetResponseStream())
    Dim responseData As String = responseReader.ReadToEnd()

    responseReader.Close()
    webRequest.GetResponse().Close()

Catch ex As WebException
    'The next line will raise a "Object reference not set to an instance of an object" exception
    Dim resp = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
    Dim obj = JsonConvert.DeserializeObject(resp)
    Return False
End Try

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2025-01-20 01:31:12

如果您使用基本身份验证,您可以尝试如下操作:

Dim strUrl As String = InternalSettings.APIurl + "/config"
Dim request As WebRequest = DirectCast(WebRequest.Create(strUrl), HttpWebRequest)
Dim strResponse As String = ""
Dim byteAuth() As Byte = Encoding.UTF8.GetBytes(InternalSettings.APIkey & ":" & InternalSettings.APIpwd)
Dim strPost As String = "SaveAllCustomersData=false"    
Dim bytePost() As Byte = Encoding.UTF8.GetBytes(strPost)

request.ContentType = "application/x-www-form-urlencoded"
request.Method = "PUT"
request.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(byteAuth))

Using sw = New StreamWriter(request.GetRequestStream())
  sw.Write(bytePost, 0, bytePost.Length)
  sw.Flush()

  Using response As HttpWebResponse = request.GetResponse()
    Using sr = New StreamReader(response.GetResponseStream())
      strResponse = sr.ReadToEnd()
    End Using
  End Using

End Using

If you are using basic authentification you can try something like this:

Dim strUrl As String = InternalSettings.APIurl + "/config"
Dim request As WebRequest = DirectCast(WebRequest.Create(strUrl), HttpWebRequest)
Dim strResponse As String = ""
Dim byteAuth() As Byte = Encoding.UTF8.GetBytes(InternalSettings.APIkey & ":" & InternalSettings.APIpwd)
Dim strPost As String = "SaveAllCustomersData=false"    
Dim bytePost() As Byte = Encoding.UTF8.GetBytes(strPost)

request.ContentType = "application/x-www-form-urlencoded"
request.Method = "PUT"
request.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(byteAuth))

Using sw = New StreamWriter(request.GetRequestStream())
  sw.Write(bytePost, 0, bytePost.Length)
  sw.Flush()

  Using response As HttpWebResponse = request.GetResponse()
    Using sr = New StreamReader(response.GetResponseStream())
      strResponse = sr.ReadToEnd()
    End Using
  End Using

End Using
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文