0

Im having a bit of a problem with my C# code and im not sure how to tackle it. Im attempting to DownloadString a webpage that only people that are logged in can see (otherwise it returns a 403 response). So I though the best way to do this was to set the HTTP header cookie value to that of a logged in users. Unfortunately it has not worked. Here's the snippet of code:

WebClient web = new WebClient();
web.Headers[HttpRequestHeader.Accept] = "*/*";
web.Headers[HttpRequestHeader.Cookie] = "User cookie";

string pagecontent = web.DownloadString("Forbidden page");

When the code is used, I still get a 403 error. I am definitely using a working cookie, i've tested it. What could be going wrong?

In addition to that, how would I grab a cookie from a webBrowser and set the webclient HTTP cookie header to that?

4

1 に答える 1

0

WebClient次のメソッドはクラスを使用しませんが、機能します。

string domain = ...
string url = ...
Cookie cookie = ...

var request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(domain, cookie);

using (var response = request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
    var responseValue = reader.ReadToEnd();
}
于 2012-10-21T02:44:56.317 に答える