I'm making a web request that redirects user to another URL and fetches the data. In the browser, it redirects properly & returns the result from redirected URL. However, it doesn't work from console application:
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url1);
webrequest.KeepAlive = true;
webrequest.Method = "GET";
webrequest.ContentType = "text/plain";
webrequest.Timeout = 20000;
WebResponse webresponse = webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
string result = loResponseStream.ReadToEnd();
Here the result I get is:
redirected.So I need to get data from href URL of anchor tag.
var matches = Regex.Matches(result, @".*?)"">(?.*?)");
Console.WriteLine(matches[0].Groups["url"].Value);
webrequest = (HttpWebRequest)WebRequest.Create(matches[0].Groups["url"].Value);
webresponse = (HttpWebResponse)webrequest.GetResponse();
enc = System.Text.Encoding.GetEncoding("utf-8");
loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
result = loResponseStream.ReadToEnd();
But I got a 403: Forbidden error in second request.
With Fiddler, response was:
SignatureDoesNotMatch
The request signature we calculated does not match the signature you provided. Check your key and signing method.47 45 54 0a 0a 74 65 78 74 2f 70 6c 61 69 6e 0a 31 33 37 35 38 36 34 31 31 32 0a 2f 61 70 70 74 68 6f 72 69 74 79 2d 73 74 61 74 69 63 2d 72 65 70 6f 72 74 73 2f 36 37 66 62 34 35 39 62 65 63 35 66 63 34 39 37 30 61 32 39 65 64 30 33 61 64 37 30 64 30 31 32 2f 65 6e 2e 6a 73 6f 6e04FB1EFE872CE953z5KnzkJ2+qzkz1Dad80OPACAvbUbQppLTBRzVp6JiPIIAEWWKi9nckBdXLk5pHzukGtsbufDT6eOIDuHnVfDGY20XMA=GET
Here Signature getting sent to second request is the same I've received in href URL of first response.
Also, both requests have header Connection: keep-alive in browser, but in the console application only the first request has that header (even after webrequest.KeepAlive = true;).