0

I am a beginner in the API ecosystem. I am using VB.NET to call API. Code used for the same given below:

        Try
            Dim s As HttpWebRequest
            Dim enc As UTF8Encoding
            Dim postdata As String
            Dim postdatabytes As Byte()

            Dim jo As New JObject
            jo.Add("apiid", objProp.Text7)
            jo.Add("apiData", objProp.Text8)

            postdata = jo.ToString()
            s = HttpWebRequest.Create(objProp.Text6)
            enc = New System.Text.UTF8Encoding()
            postdatabytes = enc.GetBytes(postdata)
            s.Method = "POST"
            s.ContentType = "application/json"
            s.ContentLength = postdatabytes.Length
            s.Headers.Add("user-name", "MjRKQGU1NypQJkxZYVpCdzJZXnpKVENmIXFGQyN6XkI=")
            s.Headers.Add("api-key", "UjMhTDZiUlE1MkhMWmM2RkclJXJhWUJTTWZDeHVEeDQ=")

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

            Dim responsedata As Stream = result.GetResponseStream
            Dim responsereader As StreamReader = New StreamReader(responsedata)
            Dim xResponse = responsereader.ReadToEnd
            Try
                Dim opData = JObject.Parse(xResponse)("apiData").ToString
                objProp = JsonConvert.DeserializeObject(Of default_prop)(opData)
            Catch ex As Exception

            End Try

        Catch myerror As OracleException

            'Status
            objProp.Text5 = "500"
            'Failure Response Message
            objProp.Text11 = "Internal Server Error..."
            db_close()

        End Try

My issue is that I could not find the proper syntax to retrieve API Response Status Code from the response. Request your guidance

riffnl
  • 3,248
  • 1
  • 19
  • 32
  • this might help you:https://stackoverflow.com/questions/1330856/getting-http-status-code-number-200-301-404-etc-from-httpwebrequest-and-ht – draz Oct 18 '22 at 12:54
  • Is there a reason you are not using "HttpClient" ? https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8 and https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpresponsemessage.statuscode?view=netframework-4.8 – granadaCoder Oct 18 '22 at 15:11
  • @granadaCoder Thanks your guidance. Will check the material and adopt the best practices. – Franklin.K.F Oct 19 '22 at 09:21
  • https://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/ – granadaCoder Oct 19 '22 at 22:32
  • @granadaCoder Thank you. I am learning lot of new things – Franklin.K.F Oct 19 '22 at 23:06

1 Answers1

0

With some small changes on your code you can do the trick as follow:

        '....... other code here

        Using stream = s.GetRequestStream()
            stream.Write(postdatabytes, 0, postdatabytes.Length)
        End Using

        Dim result As HttpWebResponse = CType(s.GetResponse(), HttpWebResponse)
        Console.WriteLine(result.StatusCode)
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16