HTTPwebrequests and cookies?

 

I have a class that will do two way interactions with webpages and it will also interact with webservices. I have a client who needs this to periodicall watch a page and gather information displayed by the page. This class would log onto the page just fine, however, cookies are required and I have no idea how they work or how to add cookies because I'm totally unknowledgeable about cookie protocols ot cookie manangement.

 

Could someone help

 

Public Class Webpost
        Private m_UserAgent As String
        Private m_PostDataPrelog As String
        Private m_LookupIP As String
        Private m_URL As String
        Private m_Accept As String
        Private m_ContentType As String

        Public Property Url() As String
            Get
                Url = m_URL
            End Get
            Set(ByVal Value As String)
                m_URL = Value
            End Set
        End Property
        Public Property UserAgent() As String
            Get
                UserAgent = m_UserAgent
            End Get
            Set(ByVal Value As String)
                m_UserAgent = Value
            End Set
        End Property
        Public Property ContentType() As String
            Get
                ContentType = m_ContentType
            End Get
            Set(ByVal Value As String)
                m_ContentType = Value
            End Set
        End Property

        Public Property PostStringPrelog() As String
            Get
                PostStringPrelog = m_PostDataPrelog
            End Get
            Set(ByVal Value As String)
                m_PostDataPrelog = Value
            End Set
        End Property

        Public Property Accept() As String
            Get
                Accept = m_Accept
            End Get
            Set(ByVal Value As String)
                m_Accept = Value
            End Set
        End Property


        Public Function PostRequest(ByRef IPstr As String) As String
            Const cRoutineName As String = "PostRequest"
            Const cPropMsg As String = " property is not set."
            PostRequest = ""
            ' Set the 'Method' property of the 'Webrequest' to 'POST'.

            If m_URL = "" Then
                MsgBox("URL" & cPropMsg, MsgBoxStyle.Exclamation, cRoutineName)
                Exit Function
            End If

            If m_ContentType = "" Then
                MsgBox("ContentType" & cPropMsg, MsgBoxStyle.Exclamation, cRoutineName)
                Exit Function
            End If


        If m_Accept = "" Then
            MsgBox("Accept" & cPropMsg, MsgBoxStyle.Exclamation, cRoutineName)
            Exit Function
        End If

        If m_UserAgent = "" Then
            MsgBox("UserAgent" & cPropMsg, MsgBoxStyle.Exclamation, cRoutineName)
            Exit Function
        End If

        Dim encoding As New ASCIIEncoding()
        Dim byte1 As Byte() 'm_PostDataPrelog
        Dim postData As String = IPstr ' Create a new string object to POST data to the Url.
        Dim PostStream As Stream = Nothing
        Dim response As HttpWebResponse = Nothing
        Dim ResponseStream As Stream = Nothing

        Try
            Dim webreq As HttpWebRequest = WebRequest.Create(m_URL)
            webreq.Method = "POST"
            webreq.UserAgent = m_UserAgent
            byte1 = encoding.GetBytes(postData)
            ' Set the content type of the data being posted.
            webreq.Accept = m_Accept
            webreq.ContentType = m_ContentType
            ' Set the content length of the string being posted.
            webreq.ContentLength = postData.Length
            PostStream = webreq.GetRequestStream()
            PostStream.Write(byte1, 0, byte1.Length)
            response = CType(webreq.GetResponse(), HttpWebResponse)
            ResponseStream = response.GetResponseStream()
            Dim reader As New StreamReader(ResponseStream)
            PostRequest = reader.ReadToEnd()
        Catch e As Exception
            MsgBox("WEB I/O Exception: " & e.Message, MsgBoxStyle.Exclamation, cRoutineName)
        End Try
        ResponseStream.Close()
        PostStream.Close()

        End Function
    End Class




Answer this question

HTTPwebrequests and cookies?