Unter Verwendung von WPF muss ich einen Aufruf mit HtmlAgilityPack.HtmlWeb machen, das einen Cookie von meiner Anwendung benötigt.
Ich habe den folgenden Code ausprobiert, bekomme aber einen Fehler:
HtmlWeb web = new HtmlWeb();
web.PreRequest += request =>
{
CookieContainer cookieContainer = new CookieContainer();
cookieContainer.Add(new Cookie("name",Application.GetCookie(uri)){Domain = uri.Host});
request.CookieContainer = cookieContainer;
return true;
};
Ich erhalte den folgenden Fehler: Eine Ausnahme vom Typ 'System.Net.CookieException' ist in System.dll aufgetreten, wurde jedoch nicht im Benutzercode behandelt
Zusätzliche Information: Der 'Value' = 'visit = "v = 1 & M" ... lang = v = 2 & lang = en-us' Teil des Cookies ist ungültig.
Hier ist, was meinen Fehler gelöst hat:
web.PreRequest += request =>
{
CookieContainer cookieContainer = new CookieContainer();
string str = Application.GetCookie(uri);
foreach (string s in str.Split(';'))
{
int charIndex = s.IndexOf('=');
string a = s.Substring(0, charIndex).Trim();
string b = s.Substring(charIndex+1, s.Length - charIndex-1);
cookieContainer.Add(new Cookie(a,b) { Domain = uri.Host });
}
request.CookieContainer = cookieContainer;
return true;
};