none
Programatically login to website vb.net... RRS feed

  • General discussion

  •   Dim response As HttpWebResponse = Nothing
            Dim receiveStream As StreamReader = Nothing

            ' Create a request using a URL that can receive a post.
            Dim request As HttpWebRequest = HttpWebRequest.Create(serviceURL)
            ' Set the Method property of the request to Selected value.
            request.Method = "GET"
            'Clear the headers and add the Basic Autherization Header
            request.Headers.Clear()
            'Setting Basic Authentication headers
            request.Headers.Add("Authorization", "Basic " + Base64Encode(strUserName + ":" + strPassword))
            request.KeepAlive = False
            request.Timeout = 600000
            request.ReadWriteTimeout = 600000
            request.AllowAutoRedirect = True
            request.ContentType = "text/html; charset=utf-8"      '"application/x-www-form-urlencoded"

            'Get response 
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            'Get the response stream into a receiveStream
            receiveStream = New StreamReader(response.GetResponseStream())
            Dim strOutput As String = receiveStream.ReadToEnd()
            receiveStream.Close()
            response.Close()
            Return strOutput

    Response i am getting is html of the login page.

            

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <title>Login</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            <link rel="stylesheet" type="text/css" media="screen" href="/public/stylesheets/secure.css" />
        </head>
        <body>

    <div id="login">

    <h1></h1>

    <form action="/login" method="POST" accept-charset="utf-8" enctype="application/x-www-form-urlencoded" ><input type="hidden" name="authenticityToken" value="59c51a0afbd7bbf80ba73d6ef4709e9054066599">


    <p id="username-field">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" value="" />
    </p>
    <p id="password-field">
    <label for="password">Password:</label>
    <input type="password" name="password" id="password" value="" />
    </p>
    <p id="remember-field">
    <input type="checkbox" name="remember" id="remember" value="true"  />
    <label for="remember">Remember me</label>
    </p>
    <p id="signin-field">
    <input type="submit" id="signin" value="Sign in" />
    </p>

    </form></div> 
        </body>
    </html>

    Web site which i am trying to log in has a windows log in pop up requesting user credentials. I want the html output of the home page(after login) not the log in.

    Any help will be appreciated.

            
    Wednesday, October 10, 2012 7:51 PM

All replies

  • You can get the fill in the form by using GetelementbyID like in the code below.  I don't think the form html is valid.  I was getting some errors so I posted the corrections below.  I read the html from a file instead of a webpage.  so my sample is different from your original code.  I did not get to finish the example.  The code doesn't send the text back to the URL.  You need to use the post data to return the form data.

    Imports System.Net Imports System.IO Imports System.Web 'add reference Microsoft HTML Object :ibrary Imports mshtml Imports System.Runtime.InteropServices Module Module1 Sub Main() Dim response As FileWebResponse = Nothing Dim receiveStream As StreamReader = Nothing Dim doc As mshtml.IHTMLDocument5 = New mshtml.HTMLDocument Dim serviceURL As String = "FILE://c:\temp\html3.html" ' Create a request using a URL that can receive a post. Dim request As FileWebRequest = HttpWebRequest.Create(serviceURL) ' Set the Method property of the request to Selected value. request.Method = "GET" 'Clear the headers and add the Basic Autherization Header response = DirectCast(request.GetResponse(), FileWebResponse) 'Get the response stream into a receiveStream receiveStream = New StreamReader(response.GetResponseStream()) Dim strOutput As String = receiveStream.ReadToEnd() Dim chararray() As Char = strOutput.ToCharArray Dim gh As GCHandle = GCHandle.Alloc(chararray, GCHandleType.Pinned) ' get address of variable Dim AddrOfMyString As IntPtr = gh.AddrOfPinnedObject() doc.write(strOutput) Dim htmldoc As mshtml.HTMLDocument = DirectCast(doc, mshtml.HTMLDocument) Dim username As HTMLInputElement = htmldoc.getElementById("username") username.innerText = "myusername" Dim password As HTMLInputElement = htmldoc.getElementById("password") username.innerText = "mypassword" receiveStream.Close() response.Close() End Sub End Module

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <title>Login</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            <link rel="stylesheet" type="text/css" media="screen" href="/public/stylesheets/secure.css" />
        </head>
        <body>
    <div id="login">
    <h1></h1>
    <form action="/login" method="post" accept-charset="utf-8" enctype="application/x-www-form-urlencoded" >
      <div>
        <input type="hidden" name="authenticityToken" value="59c51a0afbd7bbf80ba73d6ef4709e9054066599"/>
      </div>
    <p id="username-field">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" value="" />
    </p>
    <p id="password-field">
    <label for="password">Password:</label>
    <input type="password" name="password" id="password" value="" />
    </p>
    <p id="remember-field">
    <input type="checkbox" name="remember" id="remember" value="true"  />
    <label for="remember">Remember me</label>
    </p>
    <p id="signin-field">
    <input type="submit" id="signin" value="Sign in" />
    </p>
    </form></div> 
        </body>
    </html>



    jdweng

    Thursday, October 11, 2012 10:22 AM