c# forbidden.html,c# - 403 Forbidden error with HttpWebRequest - Stack Overflow

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:

SignatureDoesNotMatchThe 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;).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值