403 HttpWebRequest的Forbidden错误

时间:2013-08-07 09:44:46

标签: c# web-services http https

我正在发出一个网络请求,将用户重定向到另一个网址并获取数据。在浏览器中,它可以正确地重定向&返回重定向URL的结果。但是,它不适用于控制台应用程序:

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();

我得到的结果是:

<html><body>You are being <a href=\"..........................\">redirected</a>.</body></html>

所以我需要从锚标记的href URL获取数据。

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();

但是我在第二次请求中遇到了403:Forbidden错误。

使用Fiddler,回复是:

<?xml version="1.0" encoding="UTF-8"?> <Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and 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

此处Signature发送到第二个请求与我在第一次回复的href网址中收到的内容相同。

此外,两个请求在浏览器中都有标头Connection: keep-alive,但在控制台应用程序中,只有第一个请求具有该标头(即使在webrequest.KeepAlive = true;之后)。

0 个答案:

没有答案