Domanda

Sto creando un'applicazione per fare un paio di cose diverse su un sito web nella sezione di clienti registrati.

Per fare questo, devo mantenere una singola sessione Cookie continuo per tutte le richieste che faccio.

Finora ho collegato con successo al sito tramite HttpWebRequest e la risposta ha confermato che, per quanto sono stato in grado di riutilizzare il cookie.

Ho letto tutto SO e trovato argomenti che indicano come utilizzare un cookie all'interno della stessa funzione o classe, ma ho bisogno la possibilità di utilizzare il cookie in più diverse funzioni.

Il mio primo pensiero è stato quello di cercare di restituire il contenitore di biscotto dalla funzione di accesso iniziale e poi passarlo come parametro per ogni funzione successiva, ma io non riuscivo a farlo andare.

C'è qualcuno in grado di suggerire sia un metodo migliore o un modo per ottenere questo risultato?

È stato utile?

Soluzione

I alla fine è venuto attraverso la soluzione io stesso dopo molta lettura on-line. Sembra che il motivo per cui questo non ha funzionato la prima volta che ho provato è stato a causa di altre impostazioni che avevo sbagliato configurato.

Quindi, per chiunque altro che può incontrare lo stesso problema, spero che questo aiuta:

public const string userAgent = "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
public CookieContainer cookieJar;

private void Login_Click(object sender, EventArgs e)
{
    cookieJar = Login();
}

private CookieContainer Login()
{
    string username = txtUsername.Text;
    string password = txtPassword.Text;

    // Create a request using the provied URL. 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginPageURL);

    // Set the Method property of the request to POST.
    request.Method = "POST";

    // Set the Cookiecontainer
    CookieContainer cookieJar = new CookieContainer();
    request.CookieContainer = cookieJar;

    // Create POST data and convert it to a byte array.
    string postData = "username=" + username + "&password=" + password;
    byte[] byteArray = Encoding.UTF8.GetBytes (postData);

    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";

    // Set the User Agent
    request.UserAgent = userAgent;

    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;

    // Get the request stream.
    Stream dataStream = request.GetRequestStream ();

    // Write the data to the request stream.
    dataStream.Write (byteArray, 0, byteArray.Length);

    // Close the Stream object.
    dataStream.Close ();

    // Get the response.
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream ();

    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader (dataStream);

    // Read the content.
    string responseFromServer = reader.ReadToEnd ();

    string cookie = cookieJar.GetCookieHeader(request.RequestUri);

    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();

    return cookieJar;
}

private void viewPage(CookieContainer cookieJar, string pageURL)
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pageURL);

    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";

    // Set the Method property of the request to POST.
    request.Method = "POST";

    // Set the User Agent
    request.UserAgent = userAgent;

    // Put session back into CookieContainer
    request.CookieContainer = cookieJar;

    // Get the request stream.
    Stream dataStream = request.GetRequestStream();

    // Close the Stream object.
    dataStream.Close();

    // Get the response.
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream();

    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);

    // Read the content.
    string responseFromServer = reader.ReadToEnd();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top