How to get the next page from here?

I wrote the following code to access a https website and do some work programmatically. What I want know is how can I get the next page after finishing the login process .

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

ServicePointManager.ServerCertificateValidationCallback =

new System.Net.Security.RemoteCertificateValidationCallback(this.AcceptCertificate);

NetworkCredential nc = new NetworkCredential("x", "y");

CookieContainer cc = new CookieContainer();

HttpWebRequest myWebRequest = WebRequest.Create("https://www.x.com/Security/logon.asp") as HttpWebRequest;

Uri cookieUri = new Uri("https://www.x.com/Security/logon.asp");

myWebRequest.ContentType = "application/x-www-form-urlencoded";

myWebRequest.Credentials = nc;

myWebRequest.CookieContainer = cc;

myWebRequest.Accept = "true";

myWebRequest.AllowAutoRedirect = true;

byte[] byteArray = Encoding.ASCII.GetBytes("logonid=a&logonpswd=b&submit2=Log On");

myWebRequest.Method = "POST";

myWebRequest.ContentLength = byteArray.Length;

Stream st = myWebRequest.GetRequestStream();

st.Write(byteArray, 0, byteArray.Length);

HttpWebResponse myWebResponse = myWebRequest.GetResponse() as HttpWebResponse;

myWebResponse.Cookies = myWebRequest.CookieContainer.GetCookies(myWebResponse.ResponseUri);

StreamReader streamRead = new StreamReader(myWebResponse.GetResponseStream());

What should I write here to get the next page in the website . I wonder if I should write another webrequest with same parameters. Btw everything works fine for the certificaiton and returns true.

Thanks



Answer this question

How to get the next page from here?

  • TELII

    I solved it thanks.
  • RavindraPatil

    Ala,

    I was wondering whether you could post your working solution as I am experiencing the same problems with 2nd requests.

    Many thanks,

    Jason Hughes


  • Murali Dhanraj

    Hi,

    Thanks for the answer but I tried and it didn't work if I use another web request. Can you help me with a sample code

    Thanks alot,

    Ala


  • Mark A. Richman

    If you want to implement a complete HTTP conversation, you have to send a sequence of HttpWebRequests, navigating through the web application as required. Make sure to keep sending any session or authentication cookies by reusing your CookieContainer.
  • Ferzle

    Can you provide more information on what you are trying to achieve exactly, and what errors you are getting
  • reachrishikh

    Can you post your solution because I am also facing the exact issue which you pointed to Also my code is same like yours.

    Regards
    Mani


  • LEPScapa

    Yes, I second that request to see how you solved it, please
  • Alex Chew

    Ok I am trying to access a https website and get some information and upload some files to it. I used the previous code to sign in in the website first. Then get other pages to upload files and download some reports. Here is the complete code I used.

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                    ServicePointManager.ServerCertificateValidationCallback =
                        new System.Net.Security.RemoteCertificateValidationCallback(this.AcceptCertificate);
                    NetworkCredential nc = new NetworkCredential("a", "s");

                    CookieContainer cc = new CookieContainer();
                    Uri cookieUri = new Uri("https://www.a.com/Security/logon.asp");

                    HttpWebRequest myWebRequest = WebRequest.Create(cookieUri) as HttpWebRequest;

                    myWebRequest.ContentType = "application/x-www-form-urlencoded";
                    myWebRequest.Credentials = nc;
                    myWebRequest.CookieContainer = cc;
                    myWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                    myWebRequest.KeepAlive = true;
                    myWebRequest.AllowAutoRedirect = true;
                    myWebRequest.Timeout = 30000;

                     
                    byte[] byteArray = Encoding.UTF8.GetBytes("logonid=a&logonpswd=b&submit2=Log On&provid=&pincode=");
                    myWebRequest.Method = "POST";
                    myWebRequest.ContentLength = byteArray.Length;
                    Stream st = myWebRequest.GetRequestStream();
                    st.Write(byteArray, 0, byteArray.Length);
                    st.Close();
                    // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
                    HttpWebResponse myWebResponse = myWebRequest.GetResponse() as HttpWebResponse;
                    StreamReader streamRead = new StreamReader(myWebResponse.GetResponseStream());
                    string myResponse = streamRead.ReadToEnd();
                    streamRead.Close();


                    //2nd request
                    myWebRequest = WebRequest.Create("https://www.a.com/Upload/download.asp") as HttpWebRequest;
                    myWebRequest.CookieContainer = cc;
                    myWebRequest.Method = "GET";
                    myWebRequest.ContentType = "application/x-www-form-urlencoded";
                    myWebRequest.Credentials = nc;
                    myWebRequest.KeepAlive = true;
                    myWebRequest.AllowAutoRedirect = true;
                    myWebResponse = myWebRequest.GetResponse() as HttpWebResponse;
                    streamRead = new StreamReader(myWebResponse.GetResponseStream());
                    myResponse = streamRead.ReadToEnd();
                    streamRead.Close();


                             }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private bool AcceptCertificate(object sender,System.Security.Cryptography.X509Certificates.X509Certificate xl,
                System.Security.Cryptography.X509Certificates.X509Chain xc,System.Net.Security.SslPolicyErrors spe)
            {
               
                if ((spe & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) == System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors)
                    return false;
                else if ((spe & System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch) == System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch)
                {
                    Zone z = Zone.CreateFromUrl(((HttpWebRequest)sender).RequestUri.ToString());
                    if (z.SecurityZone == System.Security.SecurityZone.Intranet
                       || z.SecurityZone == System.Security.SecurityZone.MyComputer)
                    {
                        return true;
                    }
                    return false;
                }
                else if ((spe & System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable) == System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable)
                {
                    return false;
                }
                return true;
            }

     

    After I make the first request it redirects me to the logon and sounds the loging process is not done right. Pls take a look at this code and let me know whats wrong so I can change it.  BTW I am not getting any errors and I am not sure about using the newtwork credential.

    Thanks alot.


  • How to get the next page from here?