HttpWebRequest to login to coldfusion site.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twebb72
    New Member
    • Mar 2008
    • 2

    HttpWebRequest to login to coldfusion site.

    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...

    Code:
    private static string FormLoginGet(string loginUri, string loginData, string requestUri)
            {
                // cookieContainer is used to store the cookies used by the login
                CookieContainer cookieContainer = new CookieContainer();
    
                // First hit the login page
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(loginUri);
                req.CookieContainer = cookieContainer;
                req.Method = "POST";
                req.KeepAlive = false;
                req.AllowAutoRedirect = false;
                req.ContentType = "application/x-www-form-urlencoded|application/xml";
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] loginDataBytes = encoding.GetBytes(loginData);
                req.ContentLength = loginDataBytes.Length;
                Stream stream = req.GetRequestStream();
                stream.Write(loginDataBytes, 0, loginDataBytes.Length);
                stream.Close();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    
             
    
                // Then grab the content of the desired page
                req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
                req.AllowAutoRedirect = false;
                req.KeepAlive = false;
                req.CookieContainer = cookieContainer;
                req.Method = "GET";            
                res = (HttpWebResponse)req.GetResponse();
    
    
    
                if (res.StatusCode == HttpStatusCode.Found)
                {
                    MessageBox.Show((string)res.Headers["Location"]);
                }
    
    
                StreamReader sr = new StreamReader(res.GetResponseStream());
                return sr.ReadToEnd();
            }
    Thanks in advance! Any help would be much appreciated.
  • twebb72
    New Member
    • Mar 2008
    • 2

    #2
    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.

    Originally posted by twebb72
    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...

    Code:
    private static string FormLoginGet(string loginUri, string loginData, string requestUri)
            {
                // cookieContainer is used to store the cookies used by the login
                CookieContainer cookieContainer = new CookieContainer();
    
                // First hit the login page
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(loginUri);
                req.CookieContainer = cookieContainer;
                req.Method = "POST";
                req.KeepAlive = false;
                req.AllowAutoRedirect = false;
                req.ContentType = "application/x-www-form-urlencoded|application/xml";
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] loginDataBytes = encoding.GetBytes(loginData);
                req.ContentLength = loginDataBytes.Length;
                Stream stream = req.GetRequestStream();
                stream.Write(loginDataBytes, 0, loginDataBytes.Length);
                stream.Close();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    
             
    
                // Then grab the content of the desired page
                req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
                req.AllowAutoRedirect = false;
                req.KeepAlive = false;
                req.CookieContainer = cookieContainer;
                req.Method = "GET";            
                res = (HttpWebResponse)req.GetResponse();
    
    
    
                if (res.StatusCode == HttpStatusCode.Found)
                {
                    MessageBox.Show((string)res.Headers["Location"]);
                }
    
    
                StreamReader sr = new StreamReader(res.GetResponseStream());
                return sr.ReadToEnd();
            }
    Thanks in advance! Any help would be much appreciated.

    Comment

    Working...