473,659 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpWebRequest to login to coldfusion site.

2 New Member
Scratching my head for a while on this one... This project uses code from "Understand ing 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 1992
twebb72
2 New Member
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 "Understand ing 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
3082
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. However, I ran into a scenario that I'm baffled by. I have a website which requires a user to login. This is nothing new and I was able to successfully log in. For our case, let's say the URL to the login page is http://login.html
9
3583
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 click on a submit button. I want to automate this process by supplying values with HttpWebRequest and then download a file on the site. I think that I cannot invoke the submit button. Pleeeasee help, thanks in advance
2
380
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 trying to access the login page and simulate a login action programatically. I am running the web application on debug mode so when I send in a request I can step through to see if the data I have posted is available to the login page. The text...
2
6801
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. I want to download a file from my webapplication. I've learned that the NetworkCredential class gives a way to go but no luck. My code is as following...just dump out the web response for debugging. // C# public void Download(string username,...
2
1950
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). The request is an automated basic login post (HttpWebRequest.Method = "POST" with username and password) which successfully catches Cookies etc. But the sources code I get is the NOFRAMES part of the frameset page:
2
9680
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 "pretty quickly". The first URL I try takes a while but eventually comes back with the correct results. It took *much longer* than in Internet Explorer. The second URL times out even if I increase the timeout value significantly. Internet explorer...
2
4108
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 String, Optional ByVal ConnectionParamList As String = Nothing) As String
2
5563
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 I can download some data, but I keep getting the login page. Any ideas on this? Here's the code: ================================================== Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
1
1144
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 i need to do the same from Coldfusion to Dot.net. Given i have an encrypted value passed to Dot.Net that equals a userid and then i can get the correct password. How or where on any selected page can i authenticate the user. 2. if this is not...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.