首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Java发布登录Booking.com的请求

从Java发布登录Booking.com的请求
EN

Stack Overflow用户
提问于 2015-10-09 15:05:48
回答 1查看 824关注 0票数 0

我需要在Booking.com中登录,我正在尝试使用Java进行登录。我以不同的方式提出了一个post请求,但是我无法从de页面获得html。

这是目标页面:行政预订

我需要以下参数来登录:

loginname= password= ses= lang=en login=Login

我知道这些是参数,因为一个合作伙伴在python中登录,并且它可以工作。

se参数位于表单登录(作为一个隐藏的输入字段)上,登录名和密码由我自己提供。

所以..。为了获得se,我先发出一个get请求,然后将它作为参数添加到POST请求中。我从第一个请求中得到了没有问题的html,而不是第二个请求(POST)。

我知道POST登录resquest应该发送日志页面的html,因为正如我前面所说的,我的一个合作伙伴获得了python的结果。此外,我还在(邮递员)上尝试了这一点,它工作得很好(区别是我只提供了登录名和密码)。

这是我的代码:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class Test {

    private static final String USER_AGENT = "Mozilla/5.0";

    private static final String GET_URL = "https://admin.booking.com/hotel/hoteladmin/login.html";

    private static final String POST_URL = "https://admin.booking.com/hotel/hoteladmin/login.html";

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

        String ses = sendGET();
        System.out.println("GET DONE");
        sendPOST(ses);
        System.out.println("POST DONE");
    }

    private static String sendGET() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(GET_URL);
        httpGet.addHeader("User-Agent", USER_AGENT);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

        System.out.println("GET Response Status:: "
                + httpResponse.getStatusLine().getStatusCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();

        // print result
        Document doc = Jsoup.parse(response.toString());
        String ses = doc.select("#ses").val();
        System.out.println(response.toString());
        httpClient.close();
        return ses;
    }

    private static void sendPOST(String ses) throws IOException {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(POST_URL);
        httpPost.addHeader("User-Agent", USER_AGENT);

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("loginname", "467933"));
        urlParameters.add(new BasicNameValuePair("password", "moncloa1895"));
        urlParameters.add(new BasicNameValuePair("ses", ses));
        urlParameters.add(new BasicNameValuePair("lang", "en"));
        urlParameters.add(new BasicNameValuePair("login", "Login"));

        HttpEntity postParams = new UrlEncodedFormEntity(urlParameters);
        httpPost.setEntity(postParams);

        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

        System.out.println("POST Response Status:: "
                + httpResponse.getStatusLine().getStatusCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();

        // print result
        System.out.println(response.toString());
        httpClient.close();

    }
}

有人知道我怎么解决这个问题吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-09 17:00:04

你应该跟随重定向。添加如下内容:

代码语言:javascript
复制
httpPost.setRedirectStrategy(new LaxRedirectStrategy());

我不确定HTTPPost上的实际方法是什么,但是HttpClient有这种能力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33041602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档