0

我正在尝试使用 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();
    }
  }
} 

这个只是打印登录页面,但我想登录系统并设置 JSESSIONID 和

想检查身份验证是否正确发生。

请帮我解决这个问题。

最好的问候安托

4

1 回答 1

0

您很可能需要将凭据发布到另一个 URL 才能登录。http://localhost:8080/login.jsp这只是登录页面的地址。在该 jsp 文件中查找form应该具有action登录操作地址的属性。就像是:

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

然后将您的请求定位到该 URL ( http://localhost:8080/j_spring_security_check)。

于 2013-04-12T06:35:42.753 回答