Post 使用Jsoup发布登录&;cookie数据

Post 使用Jsoup发布登录&;cookie数据,post,cookies,web-scraping,jsoup,Post,Cookies,Web Scraping,Jsoup,我正在尝试使用java&jsoup登录我的帐户到这个网站。但无论我在这里和那里做了什么更改,我都会返回登录页面作为响应。我一定是出什么事了 我使用了Firefox的Firebug、Chrome和Fiddler来查看发送的值。我已经看过了关于jsoup上StackOverflow的其他一些答案,并且相信我遵循了正确的过程。如果有人能看到我的错误,我将非常感谢你的帮助 String loginURL = "http://members.permaculturedesigncourse.com

我正在尝试使用java&jsoup登录我的帐户到这个网站。但无论我在这里和那里做了什么更改,我都会返回登录页面作为响应。我一定是出什么事了

我使用了Firefox的Firebug、Chrome和Fiddler来查看发送的值。我已经看过了关于jsoup上StackOverflow的其他一些答案,并且相信我遵循了正确的过程。如果有人能看到我的错误,我将非常感谢你的帮助

    String loginURL = "http://members.permaculturedesigncourse.com/login";
    String dashURL = "http://members.permaculturedesigncourse.com/dashboard";
    String sUserName = "myemail@gmail.com";
    String sPass = "mypassword";

    try {

        Connection.Response login_get_res = Jsoup
                .connect(loginURL)
                .method(Connection.Method.GET)
                .execute();

        Element mySession = login_get_res.parse().getElementById("new_user_session");
        String authenticityToken = mySession.select("input[name=authenticity_token]").first().val();
        Map<String, String> cookiesFromGet = login_get_res.cookies();


        Connection.Response login_post_res = Jsoup
                .connect(loginURL)
                .data("authenticity_token", authenticityToken)
                .data("user_session[email]", sUserName)
                .data("user_session[password]", sPass)
                .data("user_session[remember_me]", "0")
                .data("x", "0")
                .data("y", "0")
                .referrer(loginURL)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36")
                .cookies(cookiesFromGet)
                .timeout(12000) 
                .method(Connection.Method.POST)
                .execute();
        Document return_page = login_post_res.parse();      
        String title = return_page.title();

    } catch (IOException e) {
        e.printStackTrace();
    }
String loginURL=”http://members.permaculturedesigncourse.com/login";
字符串dashURL=”http://members.permaculturedesigncourse.com/dashboard";
字符串sUserName=”myemail@gmail.com";
字符串sPass=“mypassword”;
试一试{
Connection.Response login\u get\u res=Jsoup
.connect(loginURL)
.method(Connection.method.GET)
.execute();
元素mySession=login\u get_res.parse().getElementById(“new\u user\u session”);
字符串authenticityToken=mySession.select(“输入[name=authenticity\u令牌]”).first().val();
Map cookiesFromGet=login\u get\u res.cookies();
Connection.Response login\u post\u res=Jsoup
.connect(loginURL)
.data(“真实性令牌”,authenticityToken)
.data(“用户会话[电子邮件]”,sUserName)
.data(“用户会话[密码]”,sPass)
.data(“用户会话[记住我],“0”)
.数据(“x”、“0”)
.数据(“y”、“0”)
.推荐人(罗吉努尔)
.userAgent(“Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/38.0.2125.101 Safari/537.36”)
.cookies(cookiesFromGet)
.超时(12000)
.method(Connection.method.POST)
.execute();
文档返回页面=login\u post\u res.parse();
字符串title=return_page.title();
}捕获(IOE异常){
e、 printStackTrace();
}

将所有数据存储在名为data的HashMap中,并将其作为数据字段提交。例如 我会:

HashMap<String, String> map = new HashMap<String, String>();
map.put("userName", userName);
map.put("password", password);
map.put("somethingelse", value);
map.put("anotherthing", anothervalue);

Document doc = null;
try {
    doc = Jsoup.connect(loginUrl).data(map).cookies(cookiesFromGet).post();
} catch (IOException e) {
    e.printStackTrace();
}
HashMap map=newhashmap();
map.put(“用户名”,用户名);
map.put(“密码”,password);
map.put(“somethingelse”,value);
map.put(“另一个东西”,另一个值);
单据单据=空;
试一试{
doc=Jsoup.connect(loginUrl).data(map).cookies(cookiesFromGet.post();
}捕获(IOE异常){
e、 printStackTrace();
}
此外,我认为.cookies()不会像您在此处尝试的那样返回cookies:

Map<String, String> cookiesFromGet = login_get_res.cookies();
Map cookiesFromGet=login\u get\u res.cookies();
我会尝试使用HttpConnection来获取cookies。像这样:

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(loginUrl);
HttpResponse response = null;
String responseString = null;
try {
    response = httpClient.execute(httpget);
    responseString = new BasicResponseHandler().handleResponse(response);
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

CookieStore cookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
HashMap<String, String> cookies = new HashMap<String, String>();
cookies = mapCookies(cookieStore.getCookies());
HttpClient-HttpClient=newdefaulthttpclient();
HttpGet HttpGet=新的HttpGet(loginUrl);
HttpResponse响应=null;
字符串responseString=null;
试一试{
response=httpClient.execute(httpget);
responseString=new BasicResponseHandler().HandlerResponse(响应);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
CookieStore CookieStore=((AbstractHttpClient)httpClient.getCookieStore();
HashMap cookies=新建HashMap();
cookies=mapCookies(cookieStore.getCookies());

最后,您可能需要对站点代码进行一些更改,以检查是否发送了POST变量,并显示用户内容而不是登录页面。我不知道您是否这样做了,因为我还没有看到您的web代码。

我感谢您的额外编辑,但您对AbstractHttpClient的引用已被弃用。我可能会改为:我很感谢您的额外编辑,但您对AbstractHttpClient的引用已被弃用。我可以通过更改为:CookieStore CookieStore=(CookieStore)((AbstractHttpClient)httpClient.getCookieStore()来获得它;但是您的mapCookie()方法无法解析。此外,我可能有误读,但这不是我的网站,我试图登录。我在另一家公司的网站上有一个帐户。因此,我无法“对站点代码进行一些更改,以检查是否发送了POST变量”