How would I go on using the logged-in session that I attained through use of Apache’s httpcomponents to utilize html (code-wise) on a site that requires you to be logged in?
This is what it looks like when I use httpcomponents:
HttpPost httpost = new HttpPost("http://whatever.com/login.php");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("email", "whatever")); //set your own username
nvps.add(new BasicNameValuePair("password", "whatever")); //set your own password
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
I assume that the answer to my problem lies somewhere with the HttpEntity class, but I feel like I am stuck..
To clarify: After I’ve logged in as shown above, I would need to access a page, with the session from above, that requires you to be logged in (e.g. membersonly.php or whatever), and then read and use the content from this page in Java.
Hope I made myself clear enough 🙂 Looking forward to any answer.
Mike
Usually, HTTP sessions are tracked through cookies received from the server which must be sent in the subsequent requests.
Just follow this example which performs form-based login.