Reputation: 875
I'm stuck on authentication to web site with webrequest on windows phone 8. I have the following code:
PostMessage = string.Concat("job=LOGIN&password=&giftCode=&language=ua&login=%D0%9D%D0%BE%D0%BC%D0%B5%D1%80+%D0%BA%D0%B0%D1%80%D1%82%D0%BA%D0%B8&actn=&custId=&dateFrom=&dateTo=&showGift=&type=&card=&trnId=&catId=&subCatId=&awrId=&ordId=&qstId=&acqId=&", "crdNo=", cardNumber, "&PIN=", pin);
this.postDataBytes = Encoding.UTF8.GetBytes(PostMessage);
public void Load()
{
HttpWebRequest loginRequest = HttpWebRequest.CreateHttp(loginUrl);
loginRequest.ContentLength = this.postDataBytes.Length;
loginRequest.Method = "POST";
loginRequest.Accept = @"text/html, application/xhtml+xml, */*";
loginRequest.ContentType = "application/x-www-form-urlencoded";
loginRequest.Headers["Host"] = "*host*";
loginRequest.Headers["Referer"] = "*refer*";
loginRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
loginRequest.CookieContainer = new CookieContainer();
loginRequest.BeginGetRequestStream(OnBeginGetRequestStream, loginRequest);
}
private void OnBeginGetRequestStream(IAsyncResult result)
{
HttpWebRequest loginRequest = result.AsyncState as HttpWebRequest;
using (Stream stream = loginRequest.EndGetRequestStream(result))
{
stream.Write(this.postDataBytes, 0, this.postDataBytes.Length);
}
loginRequest.BeginGetResponse(OnAuthenticated, loginRequest);
}
private void OnAuthenticated(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
using (WebResponse response = request.EndGetResponse(result))
{
Stream responseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream))
{
string page = reader.ReadToEnd();
}
}
}
It works sometimes... I mean this code do successfully authentication and return correct response. But sometimes it doesn't. Just ran this code few times and at some point I got correct response. I tried to use fiddler but he doesn't want track requests from emulator.
Maybe someone know the reason of this strange behavior?
Upvotes: 0
Views: 173
Reputation: 844
Did you look at this answer? I had the same problem, and the reason was cached response data.This answer is about win phone 7 but works for me on win phone 8. Httpwebrequest caching in windows phone 7
Upvotes: 1