HTTPWebRequest Login POST is not Redirecting

  • Thread starter Thread starter SirJohnnyBaze
  • Start date Start date
S

SirJohnnyBaze

Guest
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/15...EOgiNb/osbkC7LhvqfvdnHCgXlUqIwFNSJGIS2vjr3CJd"><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

Continue reading...
 
Back
Top