Pablo Varela
Pablo Varela

Reputation: 648

Get Json response from httpwebrequest in vb.net

Im working on an old VB.NET (ASP.NET) web page, and I need to get the response for an HttpWebRequest call as follows:

Dim s As HttpWebRequest
Dim enc As UTF8Encoding
Dim postdatabytes As Byte()
s = httpWebRequest.Create("www.theurl.com/api")
enc = New System.Text.UTF8Encoding()
Dim PostData = "grant_type=client_credentials"
postdata = postdata & "&client_id=" & ConfigurationManager.AppSettings("client_id")
postdata = postdata & "&client_secret=" & ConfigurationManager.AppSettings("client_secret")
postdata = postdata & "&audience=" & ConfigurationManager.AppSettings("audience")
postdatabytes = enc.GetBytes(postdata)
s.Method = "POST"
s.ContentType = "application/x-www-form-urlencoded"
s.ContentLength = postdatabytes.Length

Using stream = s.GetRequestStream()
      stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
Dim result = s.GetResponse()
response.write(result)

The problem I have is that, instead of getting the Json string I get with Postman, I get this when response writing "result":

System.Net.HttpWebResponse

Any ideas?

Thanks!

Upvotes: 2

Views: 4764

Answers (1)

Pablo Varela
Pablo Varela

Reputation: 648

For those looking for an answer, I made it work by adding the following code:

Dim responsedata As Stream = result.GetResponseStream
Dim responsereader As StreamReader = New StreamReader(responsedata)
Dim xResponse = responsereader.ReadToEnd
Response.Write(xResponse)

Thanks.

Upvotes: 1

Related Questions