[英]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. 所以我尝试了Post and Get方法,但是我不知道如何获取会话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 您可以设置用户的会话,然后通过使用来获取登录用户的ID
int id = (int)session.getid(user)
hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.