#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    64
    Rep Power
    4

    HttpWebRequest then redirect


    What I have, so far is code that logs into an https website. What I need it to do afterwards is redirect... What should I do?

    PHP Code:
    using System;
    using System.IO;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;

    namespace 
    TownClass
    {
        class 
    Program
        
    {
            public static 
    void Main(string[] args)
            {
                
    string username "userName";
                
    string password "Password";
                
    string url "https://www6.iso.com/auth/global/loginAction.do?req_url=https://www5.iso.com/cmc/app/start.do";
                
    string post_data "userId=" username "&userPassword=" password "&req_url=https://www5.iso.com/cmc/app/start.do";
                
                
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
                
                
    Uri uri = new Uri(url);
                
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                
    request.KeepAlive true;
                
    request.ProtocolVersion HttpVersion.Version10;
                
    request.Method "POST";
                
    request.MaximumAutomaticRedirections 10;
                
    request.AllowAutoRedirect true;
                
    // turn our request string into a byte stream
                
    byte[] postBytes Encoding.ASCII.GetBytes(post_data);
                
                
    // specify type
                
    request.ContentType "application/x-www-form-urlencoded";
                
    request.ContentLength postBytes.Length;
                
    Stream requestStream request.GetRequestStream();
                
                
    Console.WriteLine(request.Address);
                
                
    // now sent it
                
    requestStream.Write(postBytes0postBytes.Length);
                
    requestStream.Close();
                
                
    // grab response and print it out to the console along with the status code
                
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                
    Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
                
    Console.WriteLine(response.StatusCode);
                
                
    Console.WriteLine(request.Address);
                
    Console.Read();
            }
            
            
    // Certificate trap
            
    public static bool AcceptAllCertifications(object senderX509Certificate certificationX509Chain chainSslPolicyErrors sslPolicyErrors)
            {
                return 
    true;
            }
        }

  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,120
    Rep Power
    9398
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    64
    Rep Power
    4
    I'm using the console app to access the page. Since this, I've changed it. I'm now attempting to grab the cookie and just use it to gather the information that I need. However, the cookie isn't working, or I have something wrong.

    PHP Code:
    using System;
    using System.IO;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;

    namespace 
    TownClass
    {
        class 
    Program
        
    {
            private static 
    string username "username";
            private static 
    string password "password";
            private static 
    CookieContainer cookieContainer = new CookieContainer();
            
            public static 
    void Main(string[] args)
            {            
                
    // Set Cookie
                
    setCookie();
                
                
    // Return the state webaddresses
                
    ReturnStateInfo("https://www5.iso.com/cmc/app/start.do");
            }
            
            public static 
    void ReturnStateInfo(string url)
            {
                
    ServicePointManager.ServerCertificateValidationCallback 
                    new 
    RemoteCertificateValidationCallback(AcceptAllCertifications);
                
                
    Uri uri = new Uri(url);
                
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                
    request.CookieContainer cookieContainer// this must be set
                
    request.KeepAlive true;
                
    request.ProtocolVersion HttpVersion.Version10;
                
    request.Method "POST";
                
    request.MaximumAutomaticRedirections 10;
                
    request.AllowAutoRedirect true;
                
                
    // grab response and print it out to the console along with the status code
                
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                
                
    // For debugging purposes, show the status codes
                
    Console.WriteLine(response.StatusCode);
                
    Console.WriteLine(response.Server);
                
    Console.WriteLine(response.StatusDescription);
                
                
    // Read the response
                
    Stream answer response.GetResponseStream();
                
    StreamReader _answer = new StreamReader(answer);
                
    Console.WriteLine(_answer.ReadToEnd());
                
                
    Console.ReadLine();
            }
            
            public static 
    void setCookie()
            {
                
    string url "https://www6.iso.com/auth/global/loginAction.do";
                
    string post_data "userId=" username "&userPassword=" password;
                
                
    ServicePointManager.ServerCertificateValidationCallback 
                    new 
    RemoteCertificateValidationCallback(AcceptAllCertifications);
                
                
    Uri uri = new Uri(url);
                
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                
    request.KeepAlive true;
                
    request.ProtocolVersion HttpVersion.Version10;
                
    request.Method "POST";
                
    request.MaximumAutomaticRedirections 10;
                
    request.AllowAutoRedirect true;
                
                
    // turn our request string into a byte stream
                
    byte[] postBytes Encoding.ASCII.GetBytes(post_data);
                
                
    // specify type
                
    request.ContentType "application/x-www-form-urlencoded";
                
    request.ContentLength postBytes.Length;
                
    Stream requestStream request.GetRequestStream();
                
                
    // now sent it
                
    requestStream.Write(postBytes0postBytes.Length);
                
    requestStream.Close();
                
                
    // grab response and print it out to the console along with the status code
                
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                
                
    // set the cookie for reuse
                
    request.CookieContainer cookieContainer;
            }
            
            
            
            
            
    // Certificate trap
            
    public static bool AcceptAllCertifications(object senderX509Certificate certificationX509Chain chainSslPolicyErrors sslPolicyErrors)
            {
                return 
    true;
            } 

IMN logo majestic logo threadwatch logo seochat tools logo