问题
I just started experimenting with C# WebClient
. What I have is the code below wich gets html code from a website and writes it in a .txt file. The only problem I have is that some websites require you to accept cookies before you can use the website. What this causes is instead of writing the real website html code to the .txt file, it writes the cookie popup html code.
Code:
string downloadedString;
System.Net.WebClient client;
client = new System.Net.WebClient();
//"http://nl.wikipedia.org/wiki/Lijst_van_spelers_van_het_Nederlands_voetbalelftal"
downloadedString = client.DownloadString(textBox1.Text);
using (StreamWriter write = new StreamWriter("Data.txt"))
{
write.Write(downloadedString);
}
So what is the solution to this? Can somebody direct me to the right path?
回答1:
Usage :
CookieContainer cookieJar = new CookieContainer();
cookieJar.Add(new Cookie("my_cookie", "cookie_value", "/", "mysite"));
CookieAwareWebClient client = new CookieAwareWebClient(cookieJar);
string response = client.DownloadString("http://example.com/response_with_cookie_only.php");
public class CookieAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; set; }
public Uri Uri { get; set; }
public CookieAwareWebClient()
: this(new CookieContainer())
{
}
public CookieAwareWebClient(CookieContainer cookies)
{
this.CookieContainer = cookies;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = this.CookieContainer;
}
HttpWebRequest httpRequest = (HttpWebRequest)request;
httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
return httpRequest;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = base.GetWebResponse(request);
String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
//do something if needed to parse out the cookie.
if (setCookieHeader != null)
{
Cookie cookie = new Cookie(); //create cookie
this.CookieContainer.Add(cookie);
}
return response;
}
}
You will see two overridden methods for GetWebRequest and GetWebResponse. These methods can be overridden to handle the cookie container.
回答2:
Just store cookie string from headers into your local session _cookies string
if (System.Web.HttpContext.Current.Session["cookie"] != null)
_cookies = System.Web.HttpContext.Current.Session["cookie"].ToString();
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Cookie", _cookies);
string HtmlResult = wc.UploadString(bridge_url, myParameters);
_cookies = wc.ResponseHeaders["Set-Cookie"];
Debug.WriteLine("Headers" + _cookies);
System.Web.HttpContext.Current.Session["cookie"] = _cookies;
}
回答3:
This may be a close duplicate of How can I get the WebClient to use Cookies?
The question I referenced above is for VB.NET, but the mechanism should be the same for C#. I suspect the behavior you are seeing is the web-site is sending a cookie and then requesting it back, but your client is not setup to return the cookie to the server, so it interprets that as you 'not accepting cookies.'
Have you used a analysis tool like Fiddler to analyze what is being communicated to/from your client?
You may also have to send a particular HTTP header to indicate you accept cookies, but I don't recall that being required in my past experience.
来源:https://stackoverflow.com/questions/14551345/accept-cookies-in-webclient