Pregunta

Estoy tratando de crear una sesión HttpWebRequest / HttpWebResponse con un sitio web ASP.NET para luego analizar un formulario HTML a través de parámetros de URL (esta parte sé cómo hacerlo), pero no entiendo cómo analizar y configurar un cookie como el id de sesión. En Fiddler, muestra que el ID de sesión de ASP.NET se devuelve a través de Set-Cookie en la respuesta a la solicitud a la ruta / de la url, pero ¿cómo puedo extraer este ID de sesión y configurarlo como una cookie para la próxima HttpWebRequest? ? Entiendo que este encabezado Set-Cookie se encontraría en HttpWebResponse.Headers.Keys, pero ¿hay una ruta directa para analizarlo? ¡Gracias!

¿Fue útil?

Solución

The .NET framework will manage cookies for you. You don't have to concern yourself with parsing the cookie information out of the headers or adding a cookie header to your requests.

To store and send your session ID, use the Cookie and CookieContainer classes to store them and then make sure you send your cookies with every request.

The following example shows how to do this. The CookieContainer, 'cookieJar' can be shared across multiple domains and requests. Once you add it to a request object, the reference to it will also be added to the response object when the response is returned.

CookieContainer cookieJar = new CookieContainer();

var request = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
request.CookieContainer = cookieJar;

var response = request.GetResponse();

foreach (Cookie c in cookieJar.GetCookies(request.RequestUri))
{
    Console.WriteLine("Cookie['" + c.Name + "']: " + c.Value);
}

The output of this code will be:

Cookie['PREF']: ID=59e9a22a8cac2435:TM=1246226400:LM=1246226400:S=tvWTnbBhK4N7Tlpu

Otros consejos

The answer from Dan Herbert helped me really. I appreciate your help.

Just want to post my usage - hope it helps some one at some point of time. My requirement is that I need to send back cookies from first http post response to second http post request.

1st:

CookieContainer cookieJar = new CookieContainer();
request.CookieContainer = cookieJar;
....

CookieCollection setCookies = cookieJar.GetCookies(request.RequestUri);

2nd:

CookieContainer cc = new CookieContainer();
cc.Add(setCookies);    
request.CookieContainer = cc;

I have the same problem (with amazon) I use the following regexp:

string regexp = "(?<name>[^=]+)=(?<val>[^;]+)[^,]+,?";);
MatchCollection myMatchCollection = Regex.Matches(cookiesStr, regexp);
foreach (Match myMatch in myMatchCollection)
{
string cookieName = myMatch.Groups["name"].ToString();
string cookieVal = myMatch.Groups["val"].ToString();
Cookie cookie = new Cookie(cookieName, cookieVal);
cookies.Add(cookie);
}

Note that I only care about the cookie name/value...

good luck Elia

hum I may be wrong but from what I am observing lately

Cookies from a first response, don't include the 'set cookie' as cookies that come in the header (for example some session id...) in the case of a 302 (redirect) status

If the autofollowredirect is set to true, then the set cookie are processed, and the subsequent request which is done automatically, will include those cookies defined by set cookie on the first call

If autofollowredirect is set to false then the first request doesn't get the cookies positionned by the set cookie, and I guess and this is also my queston if anyone know, that the only way to subsequently have those cookies in next request, is parse the set cookies ?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top