私は HttpWebRequest を使用してサード パーティの Web サイトにログインを投稿する WPF のアプリケーションを持っています。HttpWebRequest.GetResponse はかなりうまく機能し、必要な正しい Cookie を取得します。
正常に動作するコードは次のとおりです。
var cookies = new CookieContainer();
string postData = "login-passaporte=email@yahoo.com.br&senha-passaporte=PASS&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();
次に、このコードを HttpClient を使用して Windows 8 ストア アプリに移植しようとすると、コードは正しいログ cookie を返しません (上記のコードでは 9 つの cookie、以下の 1 つの cookie のみ、無効なユーザー名またはパスワード)
var cookies = new CookieContainer();
string postData = "login-passaporte=email@yahoo.com.br&senha-passaporte=PASS&urlRetorno=http://sportv.globo.com/site/cartola-fc/&usar-sso=true&botaoacessar=acessar";
HttpContent content = new StringContent(postData, UTF8Encoding.UTF8);
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);
HttpClient.PostAsync が正しい情報をページに送信していないように見えますが、私が知っているほとんどすべてを試してみて、それが何であるかを理解できません。
PS.: このユーザー名とパスワードは、テスト用の作業用アカウントです。