![](/img/trans.png)
[英]apache HttpClient, form-based login, and retrieve HTML content
[英]Apache HttpClient, form-based login, Doesn´t retrieve HTML content
我正在使用 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
並從列表中刪除 action 參數。
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 存儲一起使用。 希望有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.