How to get JSESSIONID cookie in WebClient

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

Here is what I 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)); }; } 

The 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?

+5
source share
3 answers

You can save response files 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; } 
+9
source

Alternatively, the following code can be used for HttpRequest based on the following article: 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
source

Modified class for asynchronous 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; } } 
0
source

All Articles