我将 httpcomponents-client-4.2.5-bin 用于 ClientFormSubmit。我使用该示例使用 Oauth 登录到 facebook。我的 Oauth 登录有以下步骤
首次登录 facebook 使用
https://www.facebook.com/dialog/oauth?client_id=xxxxxxx&redirect_uri=http://localhost:8080/
给出它重定向到本地主机的登录详细信息,并在 url 中有 code 参数
我需要得到那个代码值。
代码是
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://www.facebook.com/dialog/oauth?client_id=358300034293206&redirect_uri=http://localhost:8080/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
HttpPost httpost = new HttpPost("https://www.facebook.com/dialog/oauth?client_id=358300034293206&redirect_uri=http://localhost:8080/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "*****"));
nvps.add(new BasicNameValuePair("pass", "*****"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Double check we've got right page " + EntityUtils.toString(entity));
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
httpclient.getConnectionManager().shutdown();
}catch(Exception e)
{
System.out.println( "Something bad just happened." );
System.out.println( e );
e.printStackTrace();
}
}
是否可以使用请求标头获取重定向 url?提前致谢。