How to make sure httpwebrequest/response is cleared on each fetch?

p_nivalis

Active member
Joined
Oct 1, 2004
Messages
33
Programming Experience
5-10
I am wondering if there is a way to be 100% certain the httpwebrequest/response is cleared so that each time it retrieves the web source, it is fresh source and not cached (or old)?

Here is the code I am currently using:
VB.NET:
    Public Function GetDataFromURL(ByVal sURL As String) As String

        GetDataFromURL = String.Empty

        Debug.Print("*GetDataFromUrl: started")
        Dim NewHTML As String = String.Empty

        sURL = Trim(sURL)

        Try

            Dim oReq As System.Net.HttpWebRequest
            Dim oResp As System.Net.HttpWebResponse

            oReq = System.Net.HttpWebRequest.Create(sURL)

            With oReq
                .Referer = RandomReferer()
                .UserAgent = RandomUserAgent()
                .Timeout = 30000
                .KeepAlive = False
                .AllowWriteStreamBuffering = True
            End With

            oResp = oReq.GetResponse

            Dim sr As New IO.StreamReader(oResp.GetResponseStream)
            Return sr.ReadToEnd

        Catch ex As Net.WebException

            Debug.Print("*WebException: " & ex.Message)

            If ex.Status = Net.WebExceptionStatus.ProtocolError Then

                Debug.Print("*Status Code: {0}", CType(ex.Response, Net.HttpWebResponse).StatusCode)
                Debug.Print("*Status Description: {0}", CType(ex.Response, Net.HttpWebResponse).StatusDescription)

                Dim strError As New System.Text.StringBuilder

                With strError
                    .Append(My.Computer.Clock.LocalTime.ToString.Trim)
                    .Append(": ")
                    .Append(ex.Message.Trim)
                End With

                ' ## Show the user the last 5 errors while attempting to retrieve source (Unfinished)
                'frmMain.ToolStripMenuItem2.Text = strError.ToString
                'frmMain.ToolStripMenuItem2.Image = frmMain.ImageList1.Images(3)

            End If

        Catch ex As Exception

            Return String.Empty
            Debug.Print("*Error Fetching URL Data: " & ex.Message)

        Finally

            Debug.Print("*GetDataFromUrl: ended")

        End Try

    End Function

Thanks in advance :)
 
You may be able to set some relevant headers for this, see the RFC about request headers (RFC 2616, HTTP/1.1 protocol). But if you see the WebHeaderCollection docs there are some protected headers that cannot be set, unless they are directly exposed as properties.
 
Back
Top