Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Cookie JSESSIONID in WebClient

I have implemented following method to get JsessioniD from the Cookies. WebSite uses form authentication.

Here is what I have implemented.

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 is implemented as follows:

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

My question is how to get only JsessionID?

like image 293
casillas Avatar asked Apr 06 '15 20:04

casillas


2 Answers

You could store the response cookies in a separate property:

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;
    }
}

and then:

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;
}
like image 69
Darin Dimitrov Avatar answered Oct 19 '22 08:10

Darin Dimitrov


Modified class for async operations only:

public class CookieAwareWebClient : WebClient
{
    public string ResponseCookies { get; private set; }

    protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
    {
        var response = base.GetWebResponse(request, result);
        this.ResponseCookies = response.Headers["Set-Cookie"];
        return response;
    }
}
like image 23
Rover Avatar answered Oct 19 '22 09:10

Rover