473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

HttpWebRequest to login to coldfusion site.

Scratching my head for a while on this one... This project uses code from "Understanding HttpWebRequest CookieContainer?" post on this site.
C#, asp.net 2.0

The following code supplies credentials to the login page properly, if I change the POST credentials, it responds with the invalid login page. On the second get request (after a successful login), it continually redirects to the main page as if the session was over. Here is the code...

Expand|Select|Wrap|Line Numbers
  1. private static string FormLoginGet(string loginUri, string loginData, string requestUri)
  2.         {
  3.             // cookieContainer is used to store the cookies used by the login
  4.             CookieContainer cookieContainer = new CookieContainer();
  5.  
  6.             // First hit the login page
  7.             HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(loginUri);
  8.             req.CookieContainer = cookieContainer;
  9.             req.Method = "POST";
  10.             req.KeepAlive = false;
  11.             req.AllowAutoRedirect = false;
  12.             req.ContentType = "application/x-www-form-urlencoded|application/xml";
  13.             ASCIIEncoding encoding = new ASCIIEncoding();
  14.             byte[] loginDataBytes = encoding.GetBytes(loginData);
  15.             req.ContentLength = loginDataBytes.Length;
  16.             Stream stream = req.GetRequestStream();
  17.             stream.Write(loginDataBytes, 0, loginDataBytes.Length);
  18.             stream.Close();
  19.             HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  20.  
  21.  
  22.  
  23.             // Then grab the content of the desired page
  24.             req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
  25.             req.AllowAutoRedirect = false;
  26.             req.KeepAlive = false;
  27.             req.CookieContainer = cookieContainer;
  28.             req.Method = "GET";            
  29.             res = (HttpWebResponse)req.GetResponse();
  30.  
  31.  
  32.  
  33.             if (res.StatusCode == HttpStatusCode.Found)
  34.             {
  35.                 MessageBox.Show((string)res.Headers["Location"]);
  36.             }
  37.  
  38.  
  39.             StreamReader sr = new StreamReader(res.GetResponseStream());
  40.             return sr.ReadToEnd();
  41.         }
  42.  
Thanks in advance! Any help would be much appreciated.
Mar 5 '08 #1
1 1978
I got it...
Step 1: Download and install Fiddler
Step 2: Realized cookie was created on the referring page, not the POST argument.
Step 3: Include a get for the referring page's cookie
Step 4: Success.

Clearly, HttpWebRequest really cannot be debugged easily without a program like fiddler. I suggest it to anyone having trouble. Lots of insight into the remote application's state.

Scratching my head for a while on this one... This project uses code from "Understanding HttpWebRequest CookieContainer?" post on this site.
C#, asp.net 2.0

The following code supplies credentials to the login page properly, if I change the POST credentials, it responds with the invalid login page. On the second get request (after a successful login), it continually redirects to the main page as if the session was over. Here is the code...

Expand|Select|Wrap|Line Numbers
  1. private static string FormLoginGet(string loginUri, string loginData, string requestUri)
  2.         {
  3.             // cookieContainer is used to store the cookies used by the login
  4.             CookieContainer cookieContainer = new CookieContainer();
  5.  
  6.             // First hit the login page
  7.             HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(loginUri);
  8.             req.CookieContainer = cookieContainer;
  9.             req.Method = "POST";
  10.             req.KeepAlive = false;
  11.             req.AllowAutoRedirect = false;
  12.             req.ContentType = "application/x-www-form-urlencoded|application/xml";
  13.             ASCIIEncoding encoding = new ASCIIEncoding();
  14.             byte[] loginDataBytes = encoding.GetBytes(loginData);
  15.             req.ContentLength = loginDataBytes.Length;
  16.             Stream stream = req.GetRequestStream();
  17.             stream.Write(loginDataBytes, 0, loginDataBytes.Length);
  18.             stream.Close();
  19.             HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  20.  
  21.  
  22.  
  23.             // Then grab the content of the desired page
  24.             req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
  25.             req.AllowAutoRedirect = false;
  26.             req.KeepAlive = false;
  27.             req.CookieContainer = cookieContainer;
  28.             req.Method = "GET";            
  29.             res = (HttpWebResponse)req.GetResponse();
  30.  
  31.  
  32.  
  33.             if (res.StatusCode == HttpStatusCode.Found)
  34.             {
  35.                 MessageBox.Show((string)res.Headers["Location"]);
  36.             }
  37.  
  38.  
  39.             StreamReader sr = new StreamReader(res.GetResponseStream());
  40.             return sr.ReadToEnd();
  41.         }
  42.  
Thanks in advance! Any help would be much appreciated.
Mar 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: omyek | last post by:
I'm trying to mimic the browsing of a webpage using an HttpWebRequest. I've had a lot of luck with it so far, including logging into pages, posting form data, and even collecting and using cookies....
9
by: buran | last post by:
Dear ASP.NET Programmers, How can I post data to an ASP.NET login page and pass authentication? The login page uses forms authentication, users must supply usernames and password and have to...
2
by: VS | last post by:
Hi I am trying to access a web application from another program using HttpWebRequest class. This web application is nothing but a web site consisting of a login page and few other pages. I'm...
2
by: TK | last post by:
I have a trouble to get web resopnse from an aspx page which is secured by Forms Authentication with custom user account database. My client application is a console application but not a browser....
2
by: Toralf | last post by:
Greetings I have trouble catching the actual HTML Source code of a HttpWebRequest when the result of a successful login is a Frameset (I cant seem to get hold of the Frameset page Source code)....
2
by: GHS | last post by:
I have some code to connect to a website and pull some content out of the HTML. I've verified that the 2 URLs I'm using are perfectly fine in Internet Explorer and both of them return results...
2
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As...
2
by: adwooley2 | last post by:
Hello. Have been losing plenty of hair over problem whereby I can't make it off the login page. Trying to pass login info to a login page and then move on to another page within the site so that...
1
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I have that ability to pass userid and password (encrypted) to a coldFusion section of a web site. I'm able to decrypt and run the login and continue through that section of the site. At this time...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.