httpposterror_java-使用HttpClient模拟HTTP POST时出现问题

在使用HttpClient尝试向http://ojp.nationalrail.co.uk发送POST请求时遇到问题,收到错误页面。代码中设置了正确的请求参数和标头,但仍然无法成功。问题可能涉及到JSESSIONID过期或其他未识别的问题。
摘要由CSDN通过智能技术生成

我正在尝试以编程方式使用HttpClient将HTTP Post请求发送到http://ojp.nationalrail.co.uk/en/s/planjourney/query,但它不喜欢我发送的请求.我从Chrome浏览器发送的内容中复制了标头和正文,因此它们是相同的,但是我不喜欢我发送的内容,因为HTML提到存在错误.

Sorry, something went wrong

There is a problem with the page you are trying to access.

It is possible that it was either moved, it doesn't exist or we are experiencing some technical difficulties.

We are sorry for the inconvenience.

这是我的使用HttpClient的Java程序:

package com.tixsnif;

import org.apache.http.*;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

import java.io.*;

import java.util.*;

import java.util.zip.GZIPInputStream;

public class WebScrapingTesting {

public static void main(String[] args) throws Exception {

String target = "http://ojp.nationalrail.co.uk/en/s/planjourney/query";

HttpClient client = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(target);

BasicNameValuePair[] params = {

new BasicNameValuePair("jpState", "single"),

new BasicNameValuePair("commandName", "journeyPlannerCommand"),

new BasicNameValuePair("from.searchTerm", "Basingstoke"),

new BasicNameValuePair("to.searchTerm", "Reading"),

new BasicNameValuePair("timeOfOutwardJourney.arrivalOrDeparture", "DEPART"),

new BasicNameValuePair("timeOfOutwardJourney.monthDay", "Today"),

new BasicNameValuePair("timeOfOutwardJourney.hour", "10"),

new BasicNameValuePair("timeOfOutwardJourney.minute", "15"),

new BasicNameValuePair("timeOfReturnJourney.arrivalOrDeparture", "DEPART"),

new BasicNameValuePair("timeOfReturnJourney.monthDay", "Today"),

new BasicNameValuePair("timeOfReturnJourney.hour", "18"),

new BasicNameValuePair("timeOfReturnJourney.minute", "15"),

new BasicNameValuePair("_includeOvertakenTrains", "on"),

new BasicNameValuePair("viaMode", "VIA"),

new BasicNameValuePair("via.searchTerm", "Station name / code"),

new BasicNameValuePair("offSetOption", "0"),

new BasicNameValuePair("_reduceTransfers", "on"),

new BasicNameValuePair("operatorMode", "SHOW"),

new BasicNameValuePair("operator.code", ""),

new BasicNameValuePair("_lookForSleeper", "on"),

new BasicNameValuePair("_directTrains", "on")};

httpPost.setHeader("Host", "ojp.nationalrail.co.uk");

httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.231 Safari/534.10");

httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch");

httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,**/*//*;q=0.8");

httpPost.setHeader("Accept-Language", "en-us,en;q=0.8");

httpPost.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

httpPost.setHeader("Origin", "http://www.nationalrail.co.uk/");

httpPost.setHeader("Referer", "http://www.nationalrail.co.uk/");

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

httpPost.setHeader("Cookie", "JSESSIONID=B2A3419B79C5D999CA4806B459675CCD.app201; Path=/");

UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(Arrays.asList(params));

urlEncodedFormEntity.setContentEncoding(HTTP.UTF_8);

httpPost.setEntity(urlEncodedFormEntity);

HttpResponse response = client.execute(httpPost);

InputStream input = response.getEntity().getContent();

GZIPInputStream gzip = new GZIPInputStream(input);

InputStreamReader isr = new InputStreamReader(gzip);

BufferedReader br = new BufferedReader(isr);

String line = null;

while((line = br.readLine()) != null) {

System.out.printf("

%s", line);

}

client.getConnectionManager().shutdown();

}

}

如果JSESSION ID过期,我会保持更新,但似乎还有另一个我看不到的问题.我是否缺少明显的东西?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值