Question Question about WebRequests and Caching

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Okay, so I'm using http web requests to make repeated calls to a certain API. The request gets sent and when the status code is OK I look at the response to determine what should be getting displayed in the log.

The problem is, sometimes I am getting blank responses. At first I thought it might be a timeout issue but that doesn't appear to be the problem. Please look at my code below

VB.NET:
                Try
                    registarUrl = "https://api.website.com/api3.xml?key=" & userKey & "&command=check&name=" & firstName.ToLower & "&duration=1"
            request = HttpWebRequest.Create(registarUrl)
                    request.Timeout = 10000

                    Dim response As HttpWebResponse = request.GetResponse()

                    If response.StatusCode = 200 Then
                        'All is fine get request stream
                        Dim datastream As Stream = response.GetResponseStream
                        Dim len As Integer = response.ContentLength
                        Dim type As String = response.ContentType

                        datastream = response.GetResponseStream
                        Dim reader As New StreamReader(datastream)
                        Dim mailaddress As String
                        Dim line As String = String.Empty
                        Dim result As String = String.Empty

                        While Not reader.EndOfStream
                            line = reader.ReadToEnd()
                            If line.Contains("<Status>success</Status>") Or line.Contains("<SuccessCode>3</SuccessCode>") = True Then
                                result = "SUCCESSFUL" 
                            ElseIf line.Contains("<Status>not_available</Status>") = True Then
                                result = "UNSUCCESSFUL"
                            Else
                                result = "ERROR"
                            End If
                        End While
                        txtLog.AppendText(firstName & "  " & result & "  " & TimeOfDay & vbCrLf)
                        datastream.Close()
                        reader.Close()
                    Else
                        txtLog.AppendText("Server returned:" & "  " & response.StatusCode & " " & response.StatusDescription & " " & TimeOfDay & vbCrLf)
                    End If
                    response.Close()
                Catch ex As Exception
                    My.Computer.FileSystem.WriteAllText(Log, Now & " EXCEPTION: " & ex.Message & vbCrLf, True)
                    request.Abort()
                End Try

As you can see, a response should always be posted into the log. Whether that response is successful, unsuccessful, or error...Something should always be posted. An example response should look like
john SUCCESSFUL 10:05:00AM

What I am noticing is that on small occasions I get no response in the log. For example:
john 10:05:00AM

After speaking with the company I use the API with..They told me I need to use caching to make sure one request has been processed before another request is sent. That tells me I'm sending the request, but apparently I get that blank response and then fire off another request which causes problems because I'm firing off a new request before that previous one has finished...

I hope this makes sense..Maybe someone can give me some help here and let me know how I would use caching to make sure I always get a response to display and never get a blank response.

Thanks!
 
Top Bottom