HTTPS WebRequest and WebResponse

  • Thread starter Marc Sartele via DotNetMonster.com
  • Start date
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:Date, 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...
 
J

Joerg Jooss

Marc said:
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?
[...]

Marc, your code implies the web application uses Basic or Digest
Authentication, but I'd rather expect Forms authentication to be used
by a secure web site. This means that you have to construct a HTTP POST
request that sends name and password like the login web form of the
bank's web site does.

Cheers,
 
G

Guest

try

my class
----------
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.IO;
using System.Text;

namespace backupbatch
{
/// <summary>
/// Summary description for http.
/// </summary>
public class http
{
ManualResetEvent wait = new ManualResetEvent(false);
CookieContainer CookieJar = new CookieContainer();
CookieCollection CookieCol = new CookieCollection();
HttpWebResponse HttpWResponse;
Uri lasturl;
string retrn = "";
int ret;

public http()
{
//
// TODO: Add constructor logic here
//
}


public string httpit(string url, string poststr, string proxy)
{
if (url.IndexOf("https") > 1)
{
ServicePointManager.CertificatePolicy = new CertPolicy();
}

if (proxy.Length > 0)
{
WebProxy proxyObject = new WebProxy(proxy.ToString()+ ":80",true);
GlobalProxySelection.Select = proxyObject;
}

HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.CookieContainer = CookieJar;
//if (retrn.Length > 0)
//{
// HttpWRequest.CookieContainer.Add(lasturl, CookieCol);
//}
//lasturl = new Uri("https://my.screenname.aol.com");
HttpWRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWRequest.UserAgent = "your mother";
HttpWRequest.KeepAlive = false;
HttpWRequest.Headers.Set("Pragma", "no-cache");
HttpWRequest.Timeout = 300000;
if(poststr.Length > 0 )
{
HttpWRequest.Method = "POST";
HttpWRequest.ContentType = "application/x-www-form-urlencoded";
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(poststr.ToString());
HttpWRequest.ContentLength = PostData.Length;
Stream tempStream = HttpWRequest.GetRequestStream();
tempStream.Write(PostData,0,PostData.Length);
tempStream.Close();
}
else
{
HttpWRequest.Method = "GET";
}


HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
retrn = HttpWResponse.Headers.ToString();
CookieJar = HttpWRequest.CookieContainer;
//CookieCol = HttpWResponse.Cookies;
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(),
Encoding.ASCII);
string s = sr.ReadToEnd();
sr.Close();
retrn = retrn + "\n\n"+s;
return retrn;
}


class CertPolicy: ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate
certificate,
WebRequest request, int certificateProblem)
{
return true;
}
}
}
}


Marc Sartele via DotNetMonster.com said:
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:Date, 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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top