使用java登录到web站点事件

使用java登录到web站点事件,java,authentication,apache-httpclient-4.x,Java,Authentication,Apache Httpclient 4.x,我尝试使用HttpClient实际登录站点,如下所示: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; im

我尝试使用HttpClient实际登录站点,如下所示:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpClientWebAPITest {
  public static void main(String[] args) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/login.jsp");

    try {

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("j_username", "mayank"));
      nameValuePairs.add(new BasicNameValuePair("j_password", "hexgen"));

      post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      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);
        if (line.startsWith("Auth=")) {
          String key = line.substring(5);
          System.out.println("key : hexgen : "+key);
          // Do something with the key
        }

      } 
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
} 
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
公共类HttpClientWebApiest{
公共静态void main(字符串[]args){
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://localhost:8080/login.jsp");
试一试{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“j_用户名”、“mayank”);
添加(新的BasicNameValuePair(“j_密码”、“hexgen”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
字符串行=”;
而((line=rd.readLine())!=null){
系统输出打印项次(行);
if(line.startsWith(“Auth=)){
字符串键=行。子字符串(5);
System.out.println(“键:hexgen:+key”);
//用钥匙做点什么
}
} 
}捕获(IOE异常){
e、 printStackTrace();
}
}
} 
这只是打印登录页面,但我想登录到系统并设置JSESSIONID和

要检查身份验证是否正确进行

请帮我解决这个问题

致意
Anto

您很可能需要将凭据发布到另一个URL才能登录<代码>http://localhost:8080/login.jsp只是登录页面的地址。在该jsp文件中查找一个
表单
,该表单应具有带有登录操作地址的
action
属性。比如:

<form action="j_spring_security_check" method="post">

然后将您的请求指向该URL(
http://localhost:8080/j_spring_security_check