java 重定向句柄_带有重定向句柄的HttpClient问题和不带浏览器的facebook登录

我花了三个星期的时间,想办法不用浏览器登录facebook主页。我编写了这个示例代码,但它返回一个错误:

org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded

信息:请求重定向,但禁用了followRedirects

import java.io.IOException;

import org.apache.commons.httpclient.Cookie;

import org.apache.commons.httpclient.Header;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.HttpState;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.httpclient.HttpVersion;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.commons.httpclient.cookie.CookiePolicy;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.params.HttpClientParams;

public class formLogin {

public static void main(String[] args) throws HttpException, IOException {

// Set target URL

String strURL = "http://www.facebook.com/login.php";;

System.out.println("Target URL: " + strURL);

// Get initial state object

HttpState initialState = new HttpState();

// Initial set of cookies can be retrieved from persistent storage

// and re-created, using a persistence mechanism of choice,

//Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff",

// "/", null, false);

// and then added to your HTTP state instance

//initialState.addCookie(mycookie);

// Get HTTP client instance

HttpClient httpclient = new HttpClient();

httpclient.getHttpConnectionManager().

getParams().setConnectionTimeout(30000);

// do not set preemptive, this forces Basic authentication which is not what we want

httpclient.getHostConfiguration().getParams()

.setParameter(HttpClientParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

httpclient.setState(initialState);

// RFC 2101 cookie management spec is used per default

// to parse, validate, format & match cookies

//httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

// A different cookie management spec can be selected

// when desired

//httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);

// Netscape Cookie Draft spec is provided for completeness

// You would hardly want to use this spec in real life situations

httpclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

// Compatibility policy is provided in order to mimic cookie

// management of popular web browsers that is in some areas

// not 100% standards compliant

// Get HTTP GET method

GetMethod httpget = new GetMethod(strURL);

// Execute HTTP GET

int result = httpclient.executeMethod(httpget);

// Display status code

System.out.println("Response status code: " + result);

// Get all the cookies

Cookie[] cookies = httpclient.getState().getCookies();

// Display the cookies

System.out.println("Present cookies: ");

for (int i = 0; i < cookies.length; i++) {

System.out.println(" - " + cookies[i].toExternalForm());

}

// Release current connection to the connection pool

// once you are done

httpget.releaseConnection();

//Cookie ist da und Jetzt wird eingeloggt

PostMethod postMethod = new PostMethod("http://facebook.com/login.php");

NameValuePair[] postData = new NameValuePair[2];

postData[0] = new NameValuePair("email", "******");

postData[1] = new NameValuePair("pass", "******");

postMethod.setRequestBody(postData);

for(int i = 0; i < cookies.length; i++){

postMethod.setRequestHeader("Cookie:",cookies[i].toExternalForm());

}

try {

httpclient.executeMethod(postMethod);

int statuscode = postMethod.getStatusCode();

System.out.println("STATUS CODE = "+statuscode);

} catch (HttpException httpe) {

System.err.print("HttpException");

System.err.println(httpe.getMessage());

httpe.printStackTrace();

} catch (IOException ioe) {

System.err.print("IOException");

System.err.println(ioe.getMessage());

ioe.printStackTrace();

}

String responseBody = postMethod.getResponseBodyAsString();

System.out.println(responseBody);

postMethod.releaseConnection();

}

}

这个密码正确吗?我需要做什么才能遵循重定向?我已经试过了

postmethod.setfollowredirect(true)

但错误依然存在。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值