VB.NET ~ PUT WebRequest raising exceptions when trying to GetResponse of it

650 Views Asked by At

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
1

There are 1 best solutions below

1
On

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