2015-10-09 40 views
0

我需要在Booking.com中登录,我正在尝试使用Java。我以不同的方式发布了一个post请求,但是我无法从de索引页获取html。发布请求从Java登录到Booking.com

这是目标页面: Admin Booking

我需要登录以下参数:

登录名= 密码= SES = LANG = EN 登录=登录

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

SES参数是关于表单登录(作为隐藏的输入字段),并通过自己提供的登录名和密码。

所以......得到SES我做之前GET请求,然后我将其添加在我的POST请求的参数。我没有问题的第一个请求,但没有第二个(POST)的HTML。

我知道POST登录resquest应该发送记录页面的html,因为正如我上面说的,我的一个合作伙伴获得python的结果。此外,我还试用了Postman Chrome应用程序(Postman),它工作正常(不同之处在于我只提供登录名和密码)。

这里是我的代码:

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(); 

    } 
} 

任何人都知道我怎样才能解决这个问题?

谢谢!

+0

你从GET和POST得到什么?参数应该是URL参数还是HTTP身体的一部分? – user489041

+0

我从GET获取登录页面的html代码,并从POST中获取任何内容(仅302状态)。这些参数应该是http身体的一部分。 – Light1988

回答

0

你应该遵循重定向。添加这样的事情:

httpPost.setRedirectStrategy(new LaxRedirectStrategy()); 

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

+1

我改变了我的代码:'CloseableHttpClient httpClient = HttpClientBuilder.create()。setRedirectStrategy(new LaxRedirectStrategy())build(); CloseableHttpResponse httpResponse = httpClient.execute(httpPost);'它的工作原理!非常感谢! – Light1988