重定向后的HttpClient响应得到错误的响应

时间:2013-09-06 20:16:33

标签: java apache-httpclient-4.x

我正在使用HttpClient version 4.2.5向网址http://lirr42.mta.info/index.php发送请求

此网址重定向到schedules.php,最后 我期待所有预定列车时刻细节的结果。

实施LaxRedirectStrategy后,我的回复代码为200,而不是302。但问题是我没有从schedules.php(重定向的网址)获得响应,而是从index.php(第一个网址)获得以下响应,仅显示我发送的内容。

  

FromStation = 56&安培; ToStation = 8&安培; RequestDate = 09%2F07%2F2013&安培; RequestAMPM = PM&安培; RequestTime = 01%3A00&安培; sortBy = 1&安培;时间表=时间表

请帮我解决问题。

public static void main(String[] args) throws Exception {
    getPageHttpClient("http://lirr42.mta.info/index.php");
}

public static String getPageHttpClient(String url) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("FromStation", "56"));
    formparams.add(new BasicNameValuePair("ToStation", "8"));
    formparams.add(new BasicNameValuePair("RequestDate", "09/07/2013"));
    formparams.add(new BasicNameValuePair("RequestAMPM", "PM"));
    formparams.add(new BasicNameValuePair("RequestTime", "01:00"));
    formparams.add(new BasicNameValuePair("sortBy", "1"));
    formparams.add(new BasicNameValuePair("schedules", "schedules"));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(entity);
    HttpContext localContext = new BasicHttpContext();
    httpclient.setRedirectStrategy(new LaxRedirectStrategy());


    HttpResponse response = httpclient.execute(httppost, localContext);
    HttpUriRequest currentReq = (HttpUriRequest) localContext.getAttribute( 
            ExecutionContext.HTTP_REQUEST);
    HttpHost currentHost = (HttpHost)  localContext.getAttribute( 
            ExecutionContext.HTTP_TARGET_HOST);
    String currentUrl = currentHost.toURI() + currentReq.getURI();        
    System.out.println(currentUrl);
    System.out.println(response);
    HttpEntity httpEntity = response.getEntity();
    String str = "";
    if (httpEntity != null) {
        str = EntityUtils.toString(entity);
        System.out.println(str);
    }
    return str;
}

计划的回应:

http://lirr42.mta.info/schedules.php 

HTTP/1.1 200 OK [Date: Fri, 06 Sep 2013 20:01:53 GMT, Server:  Apache/2.2.3 (Linux/SUSE), X-Powered-By: PHP/5.2.5, Expires: 0, Cache-Control: no-cache, Pragma: no-cache, Content-Type: text/html, Content-Length: 15832, Age: 1, Via: 1.1 localhost.localdomain]    

FromStation=56&ToStation=8&RequestDate=09%2F07%2F2013&RequestAMPM=PM&RequestTime=01%3A00&sortBy=1&schedules=schedules

1 个答案:

答案 0 :(得分:1)

你得到了正确的答案,你只是没有正确打印。

那是因为您EntityUtils.toString()而不是entity httpEntity

下面

HttpEntity httpEntity = response.getEntity();
String str = "";
if (httpEntity != null) {
    str = EntityUtils.toString(entity);
    System.out.println(str);
}

你传递的是entity

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");

即。参数。

使用

str = EntityUtils.toString(httpEntity);

获取HttpResponse内容。