我正在尝试将Java的回复发布到预订外联网。我使用POST请求正确登录但是当我尝试发送另一个POST来回复其中的特定评论时,它无法正常工作。
我尝试过这些选项:
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();
}
以上选项均无效。我从登录resquest获取了 ses 参数,而 POST_URL_REPLY 是https://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 :(得分:1)
最后我解决了。
我只需要从我的登录POST请求中获取cookie,然后将它们放入我的回复POST resquest中,它就可以了!
答案 1 :(得分: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;
}
}
}