c# - 403 Forbidden error with HttpWebRequest -
i'm making web request redirects user url , fetches data. in browser, redirects & returns result redirected url. however, doesn't work 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 result is:
<html><body>you being <a href=\"..........................\">redirected</a>.</body></html>
so need data href
url of anchor tag.
var matches = regex.matches(result, @"<a\shref=""(?<url>.*?)"">(?<text>.*?)</a>"); 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 got 403: forbidden error in second request.
with fiddler
, response was:
<?xml version="1.0" encoding="utf-8"?> <error><code>signaturedoesnotmatch</code><message>the request signature calculated not match signature provided. check key , signing method.</message><stringtosignbytes>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 6e</stringtosignbytes><requestid>04fb1efe872ce953</requestid><hostid>z5knzkj2+qzkz1dad80opacavbubqppltbrzvp6jipiiaewwki9nckbdxlk5phzu</hostid><signatureprovided>kgtsbufdt6eoiduhnvfdgy20xma=</signatureprovided><stringtosign>get
here signature
getting sent second request same i've received in href
url of first response.
also, both requests have header connection: keep-alive
in browser, in console application first request has header (even after webrequest.keepalive = true;
).
Comments
Post a Comment