none
HTTPWebRequest Login POST is not Redirecting RRS feed

  • Question

  • I need to use HTTPWebRequest to login to an external website and redirect me to a report page. When I enter the login information in the request and wait for the response i get the following message in the console:

    Request Uri was not redirected by the server.

    And when I check what the responseStream contains, I see the following:

    <HTML><HEAD><TITLE></TITLE></HEAD><BODY onLoad="document.AUTOSUBMIT.submit();

    ">This page is used to hold your data while you are being authorized for your request.<BR><BR>

    You will be forwarded to continue the authorization process.

    If this does not happen automatically, please click the Continue button below.

    <FORM NAME="AUTOSUBMIT" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded"

    ACTION="https://gcs.csfb.com/auth/nocert/1551867635/smgetcred.scc?SMQUERYDATA=-SM-w1S3I4ezM3Zn8MeA03M1CkYlrqD2FKwPl2%2buiJkP0qMG2QO%2bw6TQQrRkzsofSG36CILfCz5yC4rRnO7ubf4BRvtUXO9ML0HrbhrsTN%2bDgnd56UWw3AbAu6dFrAqt9Sk0cAFgCE5TmBOH%2bL3Pp0LKaJWjwWQGHIG6MCRSRIKjVW9Tld%2frD88rZ2T2CL3ly7P5G2fh5F7iI%2fLH4KN%2bJE%2bMwU0FJNtuJ6ZVntmkPpCRasd7SBCppfti7pRpWtKo5KJAtseF78oeXCRvqdCPwXGVcXxjK8LO%2bn%2fvF2gufOBOsJ0E4jnbHGh4gK%2fUgFKM%2fm64%2bKmDpHuCX6j9wasYzeBEOgiNb%2fosbkC7LhvqfvdnHCgXlUqIwFNSJGIS2vjr3CJd"><INPUT

    TYPE="HIDDEN"

    NAME="SMPostPreserve" VALUE="z+tWD0GoyJdxNPY0+FwKKmvTnASwNty/6jFrPuAgh07/uT2bdzqex3xsxayEwc3QZnzmNFrWnZb9Q4G9HjRGI+gARygXkAg0"><INPUT TYPE="SUBMIT" VALUE="Continue"></FORM></BODY></HTML>

    Which looks some kind of a hidden hold page, until the authentification process is finished.

    I am certain that the login information is correct. I even tried using a different proxy, ending in the same result.

    Using the reponse uri and cookies to create a new request won't help either. Any help on what I'm doing wrong?

    public static async void Login(string username, string password) { var wr = (HttpWebRequest)WebRequest.Create(myUri); wr.Method = "POST"; wr.ContentType = "application/x-www-form-urlencoded"; wr.Referer = myUri.ToString(); wr.CookieContainer = Cookies; wr.KeepAlive = true; wr.AllowAutoRedirect = true;

    var parameters = new Dictionary<string, string>{ {"username", username}, {"password", password} }; // Paste parameters into webpage using (var requestStream = wr.GetRequestStream()) { using (var writer = new StreamWriter(requestStream, Encoding.UTF8)) { writer.Write(ParamsToFormEncoded(parameters)); } } var responseTask = Task.Factory.FromAsync<WebResponse> (wr.BeginGetResponse, wr.EndGetResponse, null); using (var response = (HttpWebResponse)await responseTask) { if (response.StatusCode == HttpStatusCode.OK) Console.WriteLine("\nRequest succeeded and the requested information is in the response ,Description : {0}", response.StatusDescription); if (myUri.Equals(response.ResponseUri)) Console.WriteLine("\nThe Request Uri was not redirected by the server"); else Console.WriteLine("\nThe Request Uri was redirected to :{0}", response.ResponseUri); string responseResult = String.Empty; Stream streamResponse = response.GetResponseStream(); using (var reader = new StreamReader(streamResponse)) { responseResult = reader.ReadToEnd(); } Console.WriteLine("The response contains the following:"); Console.WriteLine(responseResult); response.Close(); } }

    Cheers

    Wednesday, March 6, 2019 10:32 AM

All replies

  • Hi SirJohnnyBaze,

    Thank you for posting here.

    For your question, you want to solve the error that’ Request Uri was not redirected by the server.’.

    You could try the following code to get correct information and the bold code is modified.

    public static async void Login(string username,string password)
            {
                var wr = (HttpWebRequest)WebRequest.Create(myUri);
                wr.Method = "POST";
                wr.ContentType = "application/x-www-form-urlencoded";
                wr.Referer = myUri.ToString();
                wr.CookieContainer = new CookieContainer();
                wr.KeepAlive = true;
                wr.AllowAutoRedirect = true;
    
                string param = "username="+username+"&password="+password;
                // Paste parameters into webpage
                using (Stream stream = wr.GetRequestStream())
                {
                    byte[] paramAsBytes = Encoding.Default.GetBytes(param);
                    stream.Write(paramAsBytes, 0, paramAsBytes.Count());
                }
    
                var responseTask = Task.Factory.FromAsync<WebResponse>
                                          (wr.BeginGetResponse,
                                           wr.EndGetResponse,
                                           null);
    
                using (var response = (HttpWebResponse)await responseTask)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                        Console.WriteLine("\nRequest succeeded and the requested information is in the response ,Description : {0}", response.StatusDescription);
    
                    if (myUri.Equals(response.ResponseUri))
                        Console.WriteLine("\nThe Request Uri was not redirected by the server");
    
                    else
                        Console.WriteLine("\nThe Request Uri was redirected to :{0}", response.ResponseUri);
    
                    string responseResult = String.Empty;
                    Stream streamResponse = response.GetResponseStream();
                    using (var reader = new StreamReader(streamResponse))
                    {
                        responseResult = reader.ReadToEnd();
                    }
                    Console.WriteLine("The response contains the following:");
                    Console.WriteLine(responseResult);
                    response.Close();
                }
            }
    

    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 7, 2019 2:22 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.
    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 12, 2019 6:22 AM
    Moderator