2015-10-30 113 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; 
     } 
    } 
}