WebResponse zurück keine cookies
Ich arbeite gerade an einer Klasse, die mir hilft, erstellen WebRequest mit cookies und POST-Parameter. Aber die Antwort.cookies ist immer leer. Wie kann ich schnappe mir den zurückgegebenen cookies und senden Sie Sie mit der nächsten Anfrage?
class WebHandler
{
private string lasturl = "";
private Dictionary<string, Cookie> cookies;
public string lastContent;
public WebHandler()
{
cookies = new Dictionary<string, Cookie>();
}
public HttpWebResponse request(string address)
{
lasturl = address;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}
request.Method = "GET";
if(lasturl != "")
request.Referer = lasturl;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie newCookie in response.Cookies)
{
Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value);
this.cookies[newCookie.Name] = newCookie;
}
lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
return response;
}
public HttpWebResponse request(string address, Dictionary<string, string> postParameters)
{
lasturl = address;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}
request.Method = "POST";
string postData = "";
foreach (string key in postParameters.Keys)
postData += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postParameters[key]) + "&";
byte[] data = Encoding.ASCII.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie newCookie in response.Cookies)
{
Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value);
this.cookies[newCookie.Name] = newCookie;
}
lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
return response;
}
}
- wenn es eine Umleitung, dann ja , werden Sie nicht bekommen, keine cookies , versuchen zu analysieren, die Antwort-Header und u werden sehen, cookies ,
Du musst angemeldet sein, um einen Kommentar abzugeben.
Seit
this.cookies
ist anfänglich null, kann Ihr code nie in die SchleifeSchlechte Teil, dass Sie nie
request.CookieContainer
null ist (Sonst würden Sie bekommen null-Verweis-Ausnahme), die zeigen würde, Sie ist etwas falsch.Seit
request.CookieContainer
null ist, HttpWebResponse bekommt nie das zurückgegeben cookies.Lösung: Einfach, fügen Sie Einfach
nach
PS: Sie Lesen möchten, können diese Frage über Namenskonventionen in c#.
PS2: in einer Methode, die
request
mit dem Namen einer Variablenrequest
nicht die Lesbarkeit zu verbessern.Ich hatte das gleiche problem, also ich habe
request.AllowAutoRedirect=false;
, jetzt kann ich cookies:Wenn die
HttpWebResponse
ObjektCookies
Sammlung leer ist, dann heißt die Antwort hatte, keine neuen cookies. Um Zugang zu allen cookies, die für die request-und response -, dann Lesen Sie die cookies, die von derHttpWebRequest
Objekt statt, wie diese: