Pregunta Cómo obtener Cookie JSESSIONID en WebClient


Implementé el siguiente método para obtener JsessioniD de las cookies. WebSite usa autenticación de formulario.

Esto es lo que he implementado.

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        using(var client= new CookieAwareWebClient())
        {
           var values= new NameValueCollection
           {
             {"username","admin"},
             {"password","admin"},
           };
            client.UploadValues("myURL/j_security_check",values);
            WebHeaderCollection myWebHeaderCollection = client.ResponseHeaders;

            for (int i=0; i < myWebHeaderCollection.Count; i++)             
Console.WriteLine ("\t" + myWebHeaderCollection.GetKey(i) + " = " + myWebHeaderCollection.Get(i));
        };
    }

CookieAwareWebClient Class se implementa de la siguiente manera:

public class CookieAwareWebClient : WebClient
  {

   public CookieAwareWebClient()
    {
       CookieContainer = new CookieContainer();
    }

    public CookieContainer CookieContainer { get; private set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = CookieContainer;
        return request;
    }
  }

enter image description here

Mi pregunta es cómo obtener solo JsessionID?


5
2018-04-06 20:30


origen


Respuestas:


Puede almacenar las cookies de respuesta en una propiedad separada:

public class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
    {
        CookieContainer = new CookieContainer();
        this.ResponseCookies = new CookieCollection();
    }

    public CookieContainer CookieContainer { get; private set; }
    public CookieCollection ResponseCookies { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = CookieContainer;
        return request;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = (HttpWebResponse)base.GetWebResponse(request);
        this.ResponseCookies = response.Cookies;
        return response;
    }
}

y entonces:

client.UploadValues("myURL/j_security_check",values);
Cookie jSessionID = client.ResponseCookies["JSESSIONID"];
if (jSessionID != null)
{
    // The server set a cookie called JSESSIONID, you can use it here:
    string value = jSessionID.Value;
}

9
2018-04-06 20:46



El siguiente código alternativo podría usarse para HttpRequest según el siguiente artículo: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer%28v=vs.110%29.aspx

private void FindCookie ()
        {
            HttpWebRequest request = WebRequest.Create (addyourURL) as HttpWebRequest;
            request.CookieContainer = new CookieContainer ();    
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "loginForm=loginForm&j_username=admin&j_password=admin";
            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // 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 = request.GetResponse () as HttpWebResponse;
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // 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 ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();

            string sid = response.Cookies["JSESSIONID"].ToString();

            Console.WriteLine(sid);
}

0
2018-04-10 21:02