HttpClient not getting same CookieContainer as
I have an application in WPF that I use HttpWebRequest to Post a login in
a 3rd party website, the HttpWebRequest.GetResponse works pretty well and
I get the correct cookies that I need.
The code working fine is:
var cookies = new CookieContainer();
string postData =
"login-passaporte=tanatopc@yahoo.com.br&senha-passaporte=tanato01&urlRetorno=http://sportv.globo.com/site/cartola-fc/&usar-sso=true&botaoacessar=acessar";
byte[] data = new UTF8Encoding().GetBytes(postData);
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("https://loginfree.globo.com/login/438");
request.Timeout = 100000;
request.CookieContainer = cookies;
request.Method = WebRequestMethods.Http.Post;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
request.AllowWriteStreamBuffering = true;
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)request.GetResponse();
Then, when I try to port this code for an Windows 8 Store App, using
HttpClient, my code do not return the correct logged cookie (9 cookies on
code above, only 1 cookie below, same cookie that I get when use invalid
username or password)
var cookies = new CookieContainer();
string postData =
"login-passaporte=tanatopc@yahoo.com.br&senha-passaporte=tanato01&urlRetorno=http://sportv.globo.com/site/cartola-fc/&usar-sso=true&botaoacessar=acessar";
byte[] data = Encoding.UTF8.GetBytes(postData);
HttpContent content = new StringContent(Convert.ToBase64String(data));
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
handler.UseCookies = true;
handler.AllowAutoRedirect = true;
var client = new HttpClient(handler);
client.MaxResponseContentBufferSize = 1024 * 1024;
client.Timeout = new TimeSpan(1000000000);
client.DefaultRequestHeaders.Add("user-agent", "Mozilla/4.0 (compatible;
MSIE 7.0; Windows NT 5.1)");
var response = await
client.PostAsync("https://loginfree.globo.com/login/438", content);
Looks to me that the HttpClient.PostAsync is not sending the correct
information to the page but I try almost everything that I know and can't
figure out what is it.
PS.: This username and password is just an working account for tests.
No comments:
Post a Comment