用httpclient获取航空公司航班信息
我想用Httpclient来获取中国东方航空公司的航班信息.但不能成功.返回的是查询页面的HTML字符串.我想它返回的是查询结果的HTML字符串.请问下面代码有什么问题?
package test;
import
java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.StringBuffer;
import java.lang.String;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.HttpStatus;
/*
* 获取中国东方航空公司的航班
*/
public class test1
{
private static final String CHARSET = "UTF-8";
public static void main(String[] args) throws
IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("www.ce-air.com",80,"http");
PostMethod post = new PostMethod("/cea2/zh_CN/info/avail?sid=3005");
NameValuePair[] data = {new NameValuePair("deptAirport", "SHA"), //出发城市上海
new NameValuePair("arrAirport", "PEK"), //到达城市北京
new NameValuePair("deptDate","20080628"),//日期
};
post.setRequestBody(data);
try
{
int statuscode = client.executeMethod(post);
InputStream ins;
BufferedReader br;
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) //判断是否有重定向地址
|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statuscode == HttpStatus.SC_SEE_OTHER)
|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT))
{
Header header = post.getResponseHeader("location"); //获取重定向的地址
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
GetMethod redirect = new GetMethod("/" + newuri); //重新执行
client.executeMethod(redirect);
ins = redirect.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(ins, CHARSET));
}
else //没有重定向地址
{
ins = post.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(ins, CHARSET));
System.err.println("没有重定向地址!");
}
StringBuffer sb = new StringBuffer();
String line = null;
while ( (line=br.readLine())!=null)
{
sb.append(line);
sb.append("\n");
}
br.close();
System.out.println(sb.toString()); //输出获取的字符串
}catch(IOException e)
{
e.printStackTrace();
}finally
{
post.releaseConnection();
}
}
}
------解决方案--------------------
这得用 firefox+firebug 或者 fidler 之类的http协议分析工具,看一下了。
一般会检测header里面的 referer 是否正确
------解决方案--------------------
------解决方案--------------------