Answer the question
In order to leave comments, you need to log in
How to get cookies when logging into a site using PCL's HttpWebRequest if the page redirects?
I decided to rewrite my library, which I wrote specifically for WP7 (event based async), on the Portable Class Library (I plan to support WStore apps in the future) using all sorts of charms like async / await. Yes, here's the problem. At the very beginning, I ran into problems, most of them had already been solved, but one remained. When entering the site, it returns 302 and the cookies I need, then it redirects to the main one. In WP7 I just set AllowAutoRedirect = false and everything worked great. But there is no such property in PCL :( Unfortunately, I did not find anything in Google. Maybe MS did some other way to disable the redirect, but I don’t know?
Well, a little question out of idle curiosity: as far as I know, there is no WebClient in PCL, it remains either to HttpWebRequest or to nuget HttpClient. I haven't really used either one yet. What is generally more convenient and for what purposes is more convenient?
Answer the question
In order to leave comments, you need to log in
They suggested a solution on the msdn forum Just
in case, I'll post a larger piece of code, suddenly it will come in handy for someone, but first you need to install the portable version of the HttpClient
Install-Package Microsoft.Net.Http
HttpClientHandler hch = new HttpClientHandler();
hch.AllowAutoRedirect = false;
HttpClient hc = new HttpClient(hch);
StringContent queryString = new StringContent(string.Format("login={0}&password={1}", Uri.EscapeUriString(username), Uri.EscapeUriString(password));
queryString.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpResponseMessage msg = await hc.PostAsync("http://www....", queryString);
string responseBody = await msg.Content.ReadAsStringAsync();
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question