简体   繁体   中英

How to get the session id after log in using java

i try to get the session after log in in a webpage, because i want to download some files after cliking in a button like download. So i tried the Post and Get method but i don't know how to get the session id. Thank you for your help

POST

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class Post {
    public static void main(String[] args) throws ClientProtocolException,
            IOException {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("https://mysite.com/new/api/login");
        StringEntity input = new StringEntity("{\"username\":\"john\",\"password\":\"doe\"}");       
        input.setContentType("application/json");
        post.setEntity(input);
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response
                .getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);

        }
                System.out.println(client);
    }
} 

GET

package post;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Get {

    public static void main(String[] args) throws ClientProtocolException,
            IOException {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("https://mysite.com/new/api/login");
        HttpResponse response = client.execute(request);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response
                .getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
                System.out.println("client");
    }  }

you can set session of a user and then get id of the user who logged in by using

int id = (int)session.getid(user)

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM