Results 1 to 4 of 4

Thread: HttpWebRequest Question

  1. #1
    sisquo76 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2008
    Location
    Bosnia and Herzegovina
    Posts
    23
    Reputation
    57

    HttpWebRequest Question

    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 by sisquo76; 01-19-2012 at 3:54 PM.

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,186
    Reputation
    2368
    Quote Originally Posted by line 38
    Dim postreqstream As Stream = postreq.GetRequestStream
    Close it when you're done with it.
    Quote Originally Posted by line 47
    postreq.GetResponse()
    Close response when you're done with it.

  3. #3
    sisquo76 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2008
    Location
    Bosnia and Herzegovina
    Posts
    23
    Reputation
    57
    Quote Originally Posted by JohnH View Post
    Close it when you're done with it.

    Close response when you're done with it.
    Sorry about late response.

    I did it like you said and it works. Thanks.

    I am logged in and redirected to /home.htm

    how to load /thsens.htm since I am logged in, because I want to extract data from that page?

    Regards

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,186
    Reputation
    2368
    Request it, use same CookieContainer object as for login.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking