gpt4 book ai didi

java - 重定向后的 HttpClient 响应收到错误响应

转载 作者:行者123 更新时间:2023-12-01 14:09:19 25 4
gpt4 key购买 nike

我正在使用 HttpClient 版本 4.2.5 向 URL 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&schedules=schedules

请帮我解决问题。

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

最佳答案

您得到了正确的响应,只是没有正确打印它。

那是因为您在 entity 上使用 EntityUtils.toString(),而不是 httpEntity

这里

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

您传递的实体

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

即。参数。

使用

str = EntityUtils.toString(httpEntity);

获取HttpResponse内容。

关于java - 重定向后的 HttpClient 响应收到错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18665933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com