Well, I've been racking my brains over this solo for too long. I could not crack it even with the hours spent on this and many other sites.
Essentially, I'm trying to remove some data from the webpage after the login page using WebRequest / Response. (I got this to work with the WebBrowser control with some multi-level events that go to different web pages, but this causes some problems when trying to refactor, not to mention that using the hidden form to work is a βbadβ practice ").
This is what I have:
string formParams = string.Format("j_username={0}&j_password={1}", userName, userPass); string cookieHeader; WebRequest request = WebRequest.Create(_signInPage); request.ContentType = "text/plain"; request.Method = "POST"; byte[] bytes = Encoding.ASCII.GetBytes(formParams); request.ContentLength = bytes.Length; using (Stream os = request.GetRequestStream()) { os.Write(bytes, 0, bytes.Length); } WebResponse response = request.GetResponse(); cookieHeader = response.Headers["Set-Cookie"]; WebRequest getRequest = WebRequest.Create(sessionHistoryPage); getRequest.Method = "GET"; getRequest.Headers.Add("Cookie", cookieHeader); WebResponse getResponse = getRequest.GetResponse(); try { using (StreamReader sr = new StreamReader(getResponse.GetResponseStream())) { textBox1.AppendText(sr.ReadToEnd()); } } catch (Exception ex) { MessageBox.Show(ex.Message); throw; }
Until now, I could go to the desired page from the first link, but when I go to the second, it sends me back to the login page, as if I had not entered.
The problem may be that cookies do not get right, but I'm new, so maybe I'm just mistaken. It captures cookies sent back from POST: JSESSIONID and S2V, however, when we go to "GET" using the FireFox WebConsole, the browser shows that it sends JSESSIONID, S2V and SPRING_SECURITY_REMEMBER_ME_COOKIE, which I believe are cookies, which are used when I click "Remember Me" in the login form.
I tried many different ways to do this using SO resources, but I still need to get to the page I need. So, for the sake of the hair that I have left, I decided to ask for help on a good ole. (This is one of those things I donβt want to miss - stubborn as it is sometimes)
If someone needs the actual address of the site I'm trying to access, I would be more than happy to send a couple of people in a private message.
The code I should reflect with Wal's suggested answer is:
var request = (HttpWebRequest)WebRequest.Create(sessionHistoryPage); request.Credentials = new NetworkCredential(userName, userPass); request.CookieContainer = new CookieContainer(); request.PreAuthenticate = true; WebResponse getResponse = request.GetResponse(); try { using (StreamReader sr = new StreamReader(getResponse.GetResponseStream())) { textBox1.AppendText(sr.ReadToEnd()); } } catch (Exception ex) { MessageBox.Show(ex.Message); throw; }
This proposal, at least the way I implemented it, did not help.
As Krizz said, I changed the code to use a CookieContainer and transferred cookies from one request to another, however the answer simply returns me to the original login page, as if I had not logged in.
Are there certain sites that simply DO NOT allow this behavior?
Final decision
The final solution was proposed by Adrian Ifod, where he stated that the website on which I am trying to log in may not allow authentication without a valid session, so adding GET to the beginning of the process allowed me to get this cookie.
Thank you all for your help!