adtteles
adtteles

Reputation: 49

HttpWebRequest.GetResponse: "Object reference not set to an instance of an object"

I receive followng error when the method GetReponse was called.

Object reference not set to an instance of an object

The code:

WebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.sample.com.br/sellerItems/123/stock");
webRequest.Method = "GET";
webRequest.Headers.Add("appToken", "ABC123456");
webRequest.ContentType = "application/json";

// Exception thrown here, webRequest is not null
using (WebResponse webResponse = webRequest.GetResponse()) 
{
    _httpStatusCode = ((HttpWebResponse)webResponse).StatusCode;
}

What else can cause that exception and how to investigate it?

I don't understand why the object webRequest (created in the first line) doesn't contain an object instance.

Upvotes: 1

Views: 9612

Answers (1)

adtteles
adtteles

Reputation: 49

To solve this the remote name RequestUri should be tested before call webRequest.GetResponse(). The method WebRequest.Create() doesn't check.

Upvotes: 1

Related Questions