2015-10-30 70 views
1

我尝试后从Java到预订外网答复。我正确地注册一个POST请求,但是当我试图发送另一个POST回复到一个特定的注释中这是行不通的。HttpPost从Java不起作用

我已经试过这些选项:

private static void sendPostReply(String ses) throws IOException { 

     HttpPost httpPost = new HttpPost(POST_URL_REPLY); 
     httpPost.addHeader("User-Agent", USER_AGENT); 

     List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();  
     urlParameters.add(new BasicNameValuePair("review_id", "xxxxxxx")); 
     urlParameters.add(new BasicNameValuePair("hotelier_response", "Some text...")); 
     urlParameters.add(new BasicNameValuePair("review_language", "es")); 
     urlParameters.add(new BasicNameValuePair("hotel_id", "xxxxx")); 
     urlParameters.add(new BasicNameValuePair("ses", ses)); 


     HttpEntity postParams = new UrlEncodedFormEntity(urlParameters); 
     httpPost.setEntity(postParams); 
     CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); 
     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(); 
     System.out.println(response); 
     httpClient.close(); 


    } 

这一个是使用JSON:

private static void sendPostReply(String ses) throws IOException { 

    HttpPost httpPost = new HttpPost(POST_URL_REPLY); 

    JSONArray array = new JSONArray(); 
    JSONObject obj = new JSONObject(); 
    obj.put("review_id", "xxxxxxx"); 
    obj.put("hotelier_response", "Some text..."); 
    obj.put("review_language", "es"); 
    obj.put("hotel_id", "xxxxx"); 
    obj.put("ses", ses); 
    array.add(obj); 

    StringEntity urlParameters = new StringEntity(array.toString(), ContentType.APPLICATION_JSON); 
    System.out.println("Parameters in JSON: " + array.toString()); 

    httpPost.setEntity(urlParameters); 
    CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); 
    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(); 
    System.out.println(response); 
    httpClient.close(); 


} 

以上选项没有一个工作。我得到的SES从登录resquest参数和POST_URL_REPLYhttps://admin.booking.com/hotel/hoteladmin/json/save_hotelier_response.html我回去为响应登录页面的HTML。

当我这样做的浏览器,这是请求和响应:

Remote Address:5.57.17.51:443 
Request URL:https://admin.booking.com/hotel/hoteladmin/json/save_hotelier_response.html 
Request Method:POST 
Status Code:200 OK 
Response Headers 
view source 
Connection:keep-alive 
Content-Encoding:gzip 
Content-Type:application/json 
Date:Thu, 29 Oct 2015 15:07:59 GMT 
Server:nginx 
Set-Cookie:esadm=02UmFuZG9tSVYkc2RlIyh9YS6XGV7%2ByXx9B9vrkkmq65tc69pqRcxf4MXcYFF70RN83kZ4HoJED%2FY%3D; domain=booking.com; path=/; expires=Tue, 27-Oct-2020 15:07:58 GMT; secure; HttpOnly 
Strict-Transport-Security:max-age=17280000 
Transfer-Encoding:chunked 
Vary:Accept-Encoding 
X-Powered-By:en104admapp-03 
X-XSS-Protection:1; mode=block 
Request Headers 
view source 
Accept:application/json, text/javascript, */*; q=0.01 
Accept-Encoding:gzip, deflate 
Accept-Language:es-ES,es;q=0.8 
Connection:keep-alive 
Content-Length:348 
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
Cookie:zz_cook_tms_seg1=2; cookie_notice_shown=1; bkng_bfp=8c9d744a312989cabdbe1ea29ae1871b; ut=e; cwd-extranet=1; slan=en; __utma=1.2120577969.1438246561.1441623510.1441874645.14; __utmz=1.1441623510.13.9.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); zz_cook_tms_seg3=5; viewedBCheckBounce=true; ecid=2IqOfbo65RGKGF3Dt8%2BQbA%2B6; __CT_Data=gpv=13&apv_382_www06=13; WRUID=0; b=%7B%22rF%22%3A%22236371895%22%7D; utag_main=v_id:014ede2ce79f000a51192387713e0606d004c06500bd0$_sn:23$_ss:1$_st:1445953128206$_pn:1%3Bexp-session$ses_id:1445951328206%3Bexp-session; bkng=11UmFuZG9tSVYkc2RlIyh9YSvtNSM2ADX0Caiw3JuH2Uo0pWsRkQnSvvtDTBR3XkdG3wfBz0e9UYwouxaoMB3pBeIxqxW4965U%2FlH8VOJg6C%2F7TC82aQuLshkxwnDownIoLYAW8pNt0m7qPo6LxV8VdxmN9xPhU%2BVVoNcokaGWUV1BMSBKfaBKGFPxL%2FWW4j3fngigWHYGOn03Ek2e8tUlPA%3D%3D; auth_token=7726476937; esadm=02UmFuZG9tSVYkc2RlIyh9YS6XGV7%2ByXx9M2MskSM2cBY2culHWUCo4gfM7hOpRzp8eT4xzpWpigQ%3D; _ga=GA1.2.2120577969.1438246561 
Host:admin.booking.com 
Origin:https://admin.booking.com 
Referer:https://admin.booking.com/hotel/hoteladmin/general/reviews.html?lang=en&ses=be8e85e657c9849afdc6beba32090217&hotel_id=xxxxx 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36 
X-Booking-CSRF:320yVgAAAAA=fw7De7XXQczccmWIprcWXDvh6g3F0Rr_aNGtKI4ZseFJpxr0XXWo9i0lPSjeMWbX4j4yV6g3GtGaBbC9RuqsdQhjV_Q1nnog9noREdwibQIlY3MStA6SzTvkpQCJ8AxirEIUzpf1HUu19fS2tbidSZkS6iabuBMc0WvRGQ 
X-FirePHP-Version:0.0.6 
X-Requested-With:XMLHttpRequest 
Form Data 
view source 
view URL encoded 
review_id:xxxxxxx 
hotelier_response:Some text... 
review_language:es 
hotel_id:xxxxxxx 
ses:be8e85e657c9849afdc6beba32090217 

和响应:

{"success":1, 
"reply_html":"   
<div class=\"hreply\">\n    
<div class=\"hreply-heading\">\n     
<i class=\"icon-calendar\"></i><p><small class=\"text-primary\"> 2015-10-29 16:07:59</small></p>\n     
<div class=\"approved\">\n     <p><small class=\"text-muted\">Waiting for approval </small></p><i class=\"icon-time\"></i>\n     
</div>\n    
</div>\n    
<div class=\"hreply-body\">\n    
<!-- Some text... -->\n    <p>Some text...</p>\n    
</div>\n   
</div>\n\n\n\n\n" 
} 

什么我做错了???

问候!

+0

我看不出有什么技术上是错误的。你发送一个请求,你会得到一个200的成功回应。按设计工作。你没有得到你想要的回复内容,但是你和网站的所有者之间存在这种内容。 – Gimby

+0

是我得到的200个OK状态,但该答复没有张贴在预订...所以它不工作。问题是......为什么?因为我在网页中发送数据。 – Light1988

+0

我可以想象该网站具有反垃圾邮件功能,可能会阻止您试图绕过使用该网站的设计 - 在浏览器中。再次说明:您必须与网站所有者进行讨论。我相信他们会很乐意听到你正在努力做到这一点。也许他们实际上有一个更好的解决方案,你可以使用而不是手动HTTP发布黑客,比如正确的web服务。 – Gimby

回答

1

最后我解决了这个问题。

我只是从我的登录POST请求的cookie,然后把它们放到我的回复发表resquest和它的作品!

0
 DefaultHttpClient httpclient = new DefaultHttpClient(); 
     DefaultRedirectStrategy redirect = new Redirect() { 
      public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) { 
       boolean isRedirect = false; 
       try { 
        isRedirect = super.isRedirected(request, response, context); 
       } catch (ProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       if (!isRedirect) { 
        int responseCode = response.getStatusLine().getStatusCode(); 
        if (responseCode == 301 || responseCode == 302) { 
         return true; 
        } 
       } 
       return isRedirect; 
      } 
     }; 
     httpclient.setRedirectStrategy(redirect); 

///////////////////

public class Redirect extends DefaultRedirectStrategy { 

    @Override 
    public HttpUriRequest getRedirect(HttpRequest request, 
             HttpResponse response, HttpContext context) 
      throws ProtocolException { 
     URI uri = getLocationURI(request, response, context); 
     String method = request.getRequestLine().getMethod(); 
     if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) { 
      return new HttpHead(uri); 
     } else { 
      HttpPost httppost = new HttpPost(uri); 
      StringEntity entity = null; 
      try { 
       entity = new StringEntity("{\"name\":\"" + _name + "\", \"ttl\":10000000000000}"); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 
      entity.setContentType("application/json"); 
      httppost.setEntity(entity); 
      return httppost; 
     } 
    } 
}