我在嘗試使用Apache HttpClient Java庫對使用基於表單的身份驗證(例如,facebook.com)的網站進行身份驗證。
使用本網站的程序爲主要例子:http://www.elitejavacoder.com/2013/10/http-client-form-based-authentication.html,我能夠做到這一點 - 但有一些我不瞭解這個程序的事情。下面是代碼:使用HttpClient進行基於表單的身份驗證 - j_security_check
package com.elitejavacoder.http.client;
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.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 HttpClientFormAuthentication {
public static void main(String[] agrs) {
String host = "yourhostname.com";
int port = 8080;
String protocol = "http";
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpHost httpHost = new HttpHost(host, port, protocol);
client.getParams().setParameter(ClientPNames.DEFAULT_HOST, httpHost);
HttpGet securedResource = new HttpGet("/secured/index.jsp");
HttpResponse httpResponse = client.execute(securedResource);
HttpEntity responseEntity = httpResponse.getEntity();
String strResponse = EntityUtils.toString(responseEntity);
int statusCode = httpResponse.getStatusLine().getStatusCode();
EntityUtils.consume(responseEntity);
System.out.println("Http status code for Unauthenticated Request: " + statusCode);// Statue code should be 200
System.out.println("Response for Unauthenticated Request: \n" + strResponse); // Should be login page
System.out.println("================================================================\n");
HttpPost authpost = new HttpPost("/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("j_username", "yourusername"));
nameValuePairs.add(new BasicNameValuePair("j_password", "yourpassword"));
authpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpResponse = client.execute(authpost);
responseEntity = httpResponse.getEntity();
strResponse = EntityUtils.toString(responseEntity);
statusCode = httpResponse.getStatusLine().getStatusCode();
EntityUtils.consume(responseEntity);
System.out.println("Http status code for Authenticattion Request: " + statusCode);// Status code should be 302
System.out.println("Response for Authenticattion Request: \n" + strResponse); // Should be blank string
System.out.println("================================================================\n");
httpResponse = client.execute(securedResource);
responseEntity = httpResponse.getEntity();
strResponse = EntityUtils.toString(responseEntity);
statusCode = httpResponse.getStatusLine().getStatusCode();
EntityUtils.consume(responseEntity);
System.out.println("Http status code for Authenticated Request: " + statusCode);// Status code should be 200
System.out.println("Response for Authenticated Request: \n" + strResponse);// Should be actual page
System.out.println("================================================================\n");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
我有以下問題(行號我要指在我上面提供的,因爲StackOverflow上不允許包含行號的鏈接的情況下):
「/ j_security_check」(第41行)究竟是什麼?作者如何知道他必須使用「j_security_check」而不是安全資源的名稱?
怎麼樣,字符串「strResponse = EntityUtils.toString(responseEntity);」 (第49行),這是「httpResponse = client.execute(authpost);」之後的兩行(第47行)不同於字符串「strResponse = EntityUtils.toString(responseEntity);」 (第59行),這是「httpResponse = client.execute(securedResource)」之後的兩行; (第57行)?
基本上,第47行和第57行之間的「客戶端」發生了什麼變化?
謝謝
您知道您的答案几乎就是這樣的完美副本:https://www.quora .COM /如何-DO-I-利用形態基於使用的身份驗證 - - HttpClient的-通j_security_check –