我正在尝试使用Apache HTTPClient在我的本地gitlab中测试一个简单的身份验证。 没有调度错误,我将重定向到登录页面:
代码:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class SimpleFormAuth {
public static void main(String[] args){
String host = "localhost";
int port = 8083;
String protocol = "http";
DefaultHttpClient client = new DefaultHttpClient();
try {
//GET para pegar os cookies ta tela de login
HttpHost httpHost = new HttpHost(host, port, protocol);
client.getParams().setParameter(ClientPNames.DEFAULT_HOST, httpHost);
HttpGet securedResource = new HttpGet("/");
HttpResponse httpResponse = client.execute(securedResource);
HttpEntity responseEntity = httpResponse.getEntity();
String strResponse = EntityUtils.toString(responseEntity);
int statusCode = httpResponse.getStatusLine().getStatusCode();
EntityUtils.consume(responseEntity);
//POST de autenticação
HttpPost authpost = new HttpPost("/users/sign_in");
authpost.addHeader("User-Agent",
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0");
authpost.addHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
authpost.addHeader("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3");
authpost.addHeader("Accept-Encoding", "gzip, deflate");
authpost.addHeader("Referer", "http://localhost:8083/users/sign_in");
authpost.addHeader("Connection", "keep-alive");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("utf8", "✓"));
nameValuePairs.add(new BasicNameValuePair("authenticity_token", "token_capturado_do_get"));
nameValuePairs.add(new BasicNameValuePair("user_login", "login_de_teste"));
nameValuePairs.add(new BasicNameValuePair("user_password", "senha_de_teste"));
nameValuePairs.add(new BasicNameValuePair("user_remember_me", "0"));
authpost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
httpResponse = client.execute(authpost);
responseEntity = httpResponse.getEntity();
strResponse = EntityUtils.toString(responseEntity);
statusCode = httpResponse.getStatusLine().getStatusCode();
EntityUtils.consume(responseEntity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用html格式更新帖子:
<form accept-charset="UTF-8" action="/users/sign_in" class="new_user" id="new_user" method="post"><div style="display:none"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="9BHyiC9DRrI0EHCeamiW0zQC4qi+eTr6KmsONEMf3PQ=" /></div><input autofocus="autofocus" class="form-control top" id="user_login" name="user[login]" placeholder="Username or Email" type="text" />
<input class="form-control bottom" id="user_password" name="user[password]" placeholder="Password" type="password" />
<div class='clearfix append-bottom-10'>
<label class='checkbox remember_me' for='user_remember_me'>
<input name="user[remember_me]" type="hidden" value="0" /><input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1" />
<span>Remember me</span>
</label>
</div>
<div>
<input class="btn-save btn" name="commit" type="submit" value="Sign in" />
<div class='pull-right'>
<a class="btn" href="/users/password/new">Forgot your password?</a>
</div>
</div>
</form>
使用表单身份验证我会在登录时获得成功吗?