Exiting an HttpClient Session

How to log out of an HttpClient session?

I use the following code to enter the application using Apache HttpClient

public HttpClient loginToHexgen(String username, String password) { HttpClient client = new DefaultHttpClient(); // send post url to login to hexgen HttpPost post = new HttpPost("http://localhost:8080/j_spring_security_check"); try { // set the user name and password List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("j_username", username)); nameValuePairs.add(new BasicNameValuePair("j_password", password)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); if (entity != null) { post.abort(); } } catch (IOException e) { e.printStackTrace(); } return client; } 

as below:

 HttpClient client = new DefaultHttpClient(); client= httpRequest.loginToHexgen("mayank", "hexgen"); 

here httpRequest is a class that uses the loginToHexgen method.

If I want to log in with multiple users with different usernames and passwords, how do I do this?

As in the same session, I want to log out of one user and log in using another user.

+6
source share
1 answer

You can use a workaround - send a request to a new user with a new cookie.

 // Create a local instance of cookie store cookieStore = new BasicCookieStore(); // Set the store httpClient.setCookieStore(cookieStore); 

The server will open a new session to a new user. Please note that the old session will NOT be closed. I do not recommend using this method.

Session management is done on the server side - you cannot do this on the client side. I recommend that you call the server URL at the end of your test, which will invalidate the server-side session. (Typically, applications that use form authentication have a logout function, and you just need to use it)

+4
source

Source: https://habr.com/ru/post/944842/


All Articles