M
Marc Sartele via DotNetMonster.com
My C# project seems a little complex for me to handle. I am trying to auto
login to a bank https site, the site will then redirect me to a menu page
and then I need to auto select a link that will build a text file and ask
if I would like to save, where I select yes to download the text file to a
local folder. I cannot get past the first step of logging into the site
automatically, any ideas?
The CODE:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Text;
namespace Web
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
///
// * create a container to hold request cookies
CookieContainer CookieJar = new CookieContainer();
///
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// Application Start
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create
("https://securebanksite.com/secure/images/index.cfm");
// Create a new HttpWebRequest Object to the mentioned URL.
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
myHttpWebRequest.GetResponse();
// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the
response:");
// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)
Console.WriteLine("\nHeader Name:{0}, Value :{1}
",myHttpWebResponse.Headers.Keys,myHttpWebResponse.Headers);
// Set 'Preauthenticate' property to true. Credentials will be sent with
the request.
myHttpWebRequest.PreAuthenticate=true;
///Manual login from console for testing
// Console.WriteLine("\nPlease Enter ur credentials for the requested Url");
// Console.WriteLine("UserName");
// string USER_ID=Console.ReadLine();
// Console.WriteLine("Password");
// string PASSWD=Console.ReadLine();
///Auto login for production
string USER_ID="username";
string PASSWD="password";
// Create a New 'NetworkCredential' object.
NetworkCredential networkCredential=new NetworkCredential(USER_ID,PASSWD);
// Associate the 'NetworkCredential' object with the 'WebRequest' object.
myHttpWebRequest.Credentials=networkCredential;
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myHttpWebRequest.GetResponse();
// Create a new HttpWebRequest Object to the mentioned URL.
myHttpWebRequest.MaximumAutomaticRedirections=2;
myHttpWebRequest.AllowAutoRedirect=true;
/// Response to screen
// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipe the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\nResponse stream received");
Char[] read = new Char[256];
// Read 256 charcters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
//while (count > 0)
//{
// Dump the 256 characters on a string and display the string onto the
console.
//String str = new String(read, 0, count);
//Console.Write(str);
//count = readStream.Read(read, 0, 256);
//}
Console.Error.WriteLine("");
// Release the resources of stream object.
readStream.Close();
// Release the resources of response object.
myWebResponse.Close();
// Releases the resources of the response.
myHttpWebResponse.Close();
/// Application End
///
}
}
}
The RESULT:
C:\Temp\bin\Debug>Web.exe
The following headers were received in the response:
Header Name:Server, Value :Netscape-Enterprise/3.6 SP3
Header Name
ate, Value :Tue, 31 May 2005 19:15:53 GMT
Header Name:Set-cookie, Value :CFID=5074066;expires=Thu, 24-May-2035
19:15:54 GMT;path=/,CFTOKEN=71606034;expires=Thu, 2
4-May-2035 19:15:54 GMT;path=/,CFID=5074066;path=/,CFTOKEN=71606034;path=/
Header Name:Content-type, Value :text/html; charset=UTF-8
Header Name:Connection, Value :close
Response stream received
HTML...
login to a bank https site, the site will then redirect me to a menu page
and then I need to auto select a link that will build a text file and ask
if I would like to save, where I select yes to download the text file to a
local folder. I cannot get past the first step of logging into the site
automatically, any ideas?
The CODE:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Text;
namespace Web
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
///
// * create a container to hold request cookies
CookieContainer CookieJar = new CookieContainer();
///
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// Application Start
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create
("https://securebanksite.com/secure/images/index.cfm");
// Create a new HttpWebRequest Object to the mentioned URL.
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
myHttpWebRequest.GetResponse();
// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the
response:");
// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)
Console.WriteLine("\nHeader Name:{0}, Value :{1}
",myHttpWebResponse.Headers.Keys,myHttpWebResponse.Headers);
// Set 'Preauthenticate' property to true. Credentials will be sent with
the request.
myHttpWebRequest.PreAuthenticate=true;
///Manual login from console for testing
// Console.WriteLine("\nPlease Enter ur credentials for the requested Url");
// Console.WriteLine("UserName");
// string USER_ID=Console.ReadLine();
// Console.WriteLine("Password");
// string PASSWD=Console.ReadLine();
///Auto login for production
string USER_ID="username";
string PASSWD="password";
// Create a New 'NetworkCredential' object.
NetworkCredential networkCredential=new NetworkCredential(USER_ID,PASSWD);
// Associate the 'NetworkCredential' object with the 'WebRequest' object.
myHttpWebRequest.Credentials=networkCredential;
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myHttpWebRequest.GetResponse();
// Create a new HttpWebRequest Object to the mentioned URL.
myHttpWebRequest.MaximumAutomaticRedirections=2;
myHttpWebRequest.AllowAutoRedirect=true;
/// Response to screen
// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipe the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\nResponse stream received");
Char[] read = new Char[256];
// Read 256 charcters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
//while (count > 0)
//{
// Dump the 256 characters on a string and display the string onto the
console.
//String str = new String(read, 0, count);
//Console.Write(str);
//count = readStream.Read(read, 0, 256);
//}
Console.Error.WriteLine("");
// Release the resources of stream object.
readStream.Close();
// Release the resources of response object.
myWebResponse.Close();
// Releases the resources of the response.
myHttpWebResponse.Close();
/// Application End
///
}
}
}
The RESULT:
C:\Temp\bin\Debug>Web.exe
The following headers were received in the response:
Header Name:Server, Value :Netscape-Enterprise/3.6 SP3
Header Name

Header Name:Set-cookie, Value :CFID=5074066;expires=Thu, 24-May-2035
19:15:54 GMT;path=/,CFTOKEN=71606034;expires=Thu, 2
4-May-2035 19:15:54 GMT;path=/,CFID=5074066;path=/,CFTOKEN=71606034;path=/
Header Name:Content-type, Value :text/html; charset=UTF-8
Header Name:Connection, Value :close
Response stream received
HTML...