java - Apache HttpClient,基于表单的登录,不检索 HTML 内容

标签 java apache-httpclient-4.x

我正在使用 apache 的 Httpclient 来访问基于表单的登录身份验证背后的另一个页面,但是,我收到的结果是相同的基于表单的登录页面。

我应该做一些不同的事情吗?

public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://localhost/salvaurls/");

    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>(4);
    params.add(new BasicNameValuePair("return_url", "http://localhost/salvaurls/painel.php")); 
    params.add(new BasicNameValuePair("action", "http://localhost/salvaurls/autenticacao.php?acao=login"));
    params.add(new BasicNameValuePair("login", "desenvolvimento@gmail.com.br"));
    params.add(new BasicNameValuePair("senha", "123"));
    UrlEncodedFormEntity entity_ = new UrlEncodedFormEntity(params, Consts.UTF_8);
    httppost.setEntity(entity_);

    //Execute and get the response.
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
        try (InputStream is = entity.getContent()) {
            System.out.println("entrou");
            Thread.sleep(2000);

            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str ="";
            while ((str = br.readLine()) != null){
                System.out.println(""+str);
            }                
        }
    }
}

index.html - 运行代码后显示的登录表单:

<body id="LoginForm">
    <div class="container">
        <h1 class="form-heading">login Form</h1>
        <div class="login-form">
            <div class="main-div">
                <div class="panel">
                    <h2>Salva URLs</h2>
                    <p>Please enter your email and password</p>
                </div>
                <form id="Login" action="autenticacao.php?acao=login" method="post">
                    <div class="form-group">
                        <input type="email" class="form-control" id="login" name="login"
                            placeholder="Email Address">
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control" id="senha" name="senha"
                            placeholder="Password">
                    </div>
                    <div class="forgot">
                        <a href="reset.html">Forgot password?</a>
                    </div>
                    <button type="submit" class="btn btn-primary">Login</button>
                </form>
            </div>
        </div>
    </div>
    </div>
</body>

最佳答案

您根本没有调用登录网址。将 HttpPost url 更改为 http://localhost/salvaurls/autenticacao.php?acao=login 并从列表中删除操作参数。

HttpPost httppost = new HttpPost("http://localhost/salvaurls/autenticacao.php?acao=login");

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("return_url", "http://localhost/salvaurls/painel.php"));
params.add(new BasicNameValuePair("login", "desenvolvimento@gmail.com.br"));
params.add(new BasicNameValuePair("senha", "123"));

通常,如果您已成功通过身份验证,服务器会发送 cookie。对于进一步的请求,您可能需要 cookie 存储。

CookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
HttpClient client = HttpClients.createDefault();

HttpPost httppost = new HttpPost("http://localhost/salvaurls/autenticacao.php?acao=login");

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("return_url", "http://localhost/salvaurls/painel.php"));
params.add(new BasicNameValuePair("login", "desenvolvimento@gmail.com.br"));
params.add(new BasicNameValuePair("senha", "123"));
UrlEncodedFormEntity entity_ = new UrlEncodedFormEntity(params, Consts.UTF_8);
httppost.setEntity(entity_);

HttpResponse response = client.execute(httppost, context);
String response = EntityUtils.toString(response.getEntity());

如果您要进行进一步的身份验证请求,则每次都需要将 HttpClientContext 与 cookie 存储一起使用。希望有帮助。

关于java - Apache HttpClient,基于表单的登录,不检索 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54709551/

相关文章:

java - Jooq (SQLite) 未插入

java - 从火力地堡检索数据

java - 评估二叉树 Java 中的表达式

java - 我可以在没有 Commons-logging.jar 的情况下使用 Apache HTTPClient 吗?

Java - 我可以使用浏览器 cookie 直接访问网站上的数据吗?

java - PoolingHttpClientConnectionManager 和 PoolingClientConnectionManager 有什么区别

java - 记录 System.out.println 在 JBoss 中迷失

java - 这种情况下最好的数据结构和算法是什么?

Java Multipart/post 下载

java - 连接和连接请求超时