views:

267

answers:

2

Hello,

I've been googling for some VB.Net code to authenticate to a web server with the POST method, receive a session ID in a cookie, and then send this cookie along with all GET queries... but all I found is half-working code or C# code, ie. difficult to use for a VB.Net newbie.

Would someone have some code handy or some pointer that I could use to get started?

Thank you.

A: 

(I have to add a pseudo-answer since a Comment doesn't seem to preserve formatting).

I can now send a POST query and get a cookie, but I'm stuck at how to read the BODY part of the response:

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

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim Stream As Stream
      Dim tempCookie As Cookie
      Dim CookieJar As New CookieContainer
      Dim CookieList As New CookieCollection
      Dim PostData As String

      Const ConnectURL = "http://www.acme.com/logon.php"

      Try
        Dim Request As HttpWebRequest = HttpWebRequest.Create(ConnectURL)
        Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
        Request.CookieContainer = CookieJar
        Request.AllowAutoRedirect = False

        'Turn POST login string into ASCII and insert into Request... somehow
        Request.ContentType = "application/x-www-form-urlencoded"
        PostData = "username=jdoe&password=test"
        Request.Method = "POST"
        Request.ContentLength = PostData.Length
        Dim requestStream As Stream = Request.GetRequestStream()
        Dim postBytes As Byte() = Encoding.ASCII.GetBytes(PostData)
        requestStream.Write(postBytes, 0, postBytes.Length)
        requestStream.Close()

        'POST query, and display cookies
        Dim Response As HttpWebResponse = Request.GetResponse()
        For Each tempCookie In Response.Cookies
            CookieJar.Add(tempCookie)
            RichTextBox1.AppendText(tempCookie.ToString)
        Next

        'How to display BODY from response stream?
        Stream = Response.GetResponseStream
        Dim ResponseData As String
        ResponseData = Stream.Read(how to read)?
        RichTextBox1.AppendText(ResponseData)

      Catch ex As Exception
          MsgBox(ex.Message.ToString)
      End Try

End Class

If some VB.Net developer knows how to do this, I'd be most grateful. Thank you.

OverTheRainbow
A: 

For those interested in doing some screen scraping, here's some working code to POST a login/password, get a session ID in a cookie, and GET other pages from the site:

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

Public Class Form1
    Const ConnectURL = "http://www.acme.com/logon.php"
    Const HomeURL = "http://www.acme.com"

    Private Function RegularPage(ByVal URL As String, ByVal CookieJar As CookieContainer) As String
        Dim reader As StreamReader

        Dim Request As HttpWebRequest = HttpWebRequest.Create(URL)
        Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
        Request.AllowAutoRedirect = False
        Request.CookieContainer = CookieJar

        Dim Response As HttpWebResponse = Request.GetResponse()

        reader = New StreamReader(Response.GetResponseStream())
        Return reader.ReadToEnd()
        Response.Close()
    End Function

    Private Sub LogonPage(ByVal URL As String, ByRef CookieJar As CookieContainer, ByVal PostData As String)
        Dim reader As StreamReader

        Dim Request As HttpWebRequest = HttpWebRequest.Create(URL)

        Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
        Request.CookieContainer = CookieJar
        Request.AllowAutoRedirect = False
        Request.ContentType = "application/x-www-form-urlencoded"
        PostData = "username=isiria&password=ceciestunmdp"
        Request.Method = "POST"
        Request.ContentLength = PostData.Length

        Dim requestStream As Stream = Request.GetRequestStream()
        Dim postBytes As Byte() = Encoding.ASCII.GetBytes(PostData)

        requestStream.Write(postBytes, 0, postBytes.Length)
        requestStream.Close()

        Dim Response As HttpWebResponse = Request.GetResponse()

        For Each tempCookie In Response.Cookies
            CookieJar.Add(tempCookie)
        Next

        reader = New StreamReader(Response.GetResponseStream())
        Response.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim CookieJar As New CookieContainer
        Dim PostData As String

        Try
            'Logon
            PostData = "username=jdoe&password=test"
            LogonPage(ConnectURL, CookieJar, PostData)

            'Homepage
            RichTextBox1.AppendText(RegularPage(HomeURL, CookieJar))
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
End Class
OverTheRainbow