Question HttpWebRequest Question

sisquo76

Active member
Joined
Dec 1, 2008
Messages
28
Location
Bosnia and Herzegovina
Programming Experience
3-5
Dear all,

I am connecting to web interface of a device which has username and password to authenticate. I managed to authenticate and load source of HTML page which i need. I need it to extract some data out of it. My code is as follows:

Imports System.Net
Imports System.Text
Imports System.IO

Public Class Form1
    Dim logincookie As CookieContainer
    

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Try
            Dim postData As String = "login_username=blabla&login_password=blabla&submit=Log+On"
            'Dim tempcookies As New CookieContainer
            Dim encoding As New UTF8Encoding
            Dim byteData As Byte() = encoding.GetBytes(postData)
            Dim postreq As HttpWebRequest = HttpWebRequest.Create("http://interface_address/Forms/login1")
            Dim cr As New System.Net.NetworkCredential("username", "password", "domain")
            Dim pr As New System.Net.WebProxy("proxy_address", 8080)
            pr.Credentials = cr
            postreq.Proxy = pr
            postreq.Method = "POST"
            postreq.KeepAlive = True
            'postreq.CookieContainer = tempcookies
            postreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729"
            postreq.ContentType = "application/x-www-form-urlencoded"
            postreq.Referer = "http://interface_address/thsens.htm"
            postreq.ContentLength = byteData.Length
            postreq.AllowAutoRedirect = True
            postreq.MaximumAutomaticRedirections = 1000
            'postreq.Timeout = 5000
            Dim postreqstream As Stream = postreq.GetRequestStream

            postreqstream.Write(byteData, 0, byteData.Length)

            Dim postresponse As HttpWebResponse
            postresponse = DirectCast(postreq.GetResponse, HttpWebResponse)

            Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
            'tempcookies.Add(postresponse.Cookies)
            'logincookie = tempcookies
            Dim thepage As String = postreqreader.ReadToEnd


            Me.TextBox1.Text = thepage
            postreq = HttpWebRequest.Create("http://interface_address/logout.htm")
            postreq.Proxy = pr
            postreq.GetResponse()
            postresponse.Close()
            'postreqstream.Close()
            postreqreader.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try        
    End Sub
End Class


I want to refresh textbox every, lets say, 15 seconds. When i press button it refreshes textbox, second time too, but third time it freezes on

Dim postreqstream As Stream = postreq.GetRequestStream


I wonder why, and how to solve this issue.

Thanks in advance
 
Last edited:
line 38 said:
Dim postreqstream As Stream = postreq.GetRequestStream
Close it when you're done with it.
line 47 said:
postreq.GetResponse()
Close response when you're done with it.
 
Request it, use same CookieContainer object as for login.
 
Back
Top