我想用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();
}

    }
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【hallquixote】截止到2008-06-28 14:18:59的历史汇总数据(不包括此帖):
    发帖数:1                  发帖分:30                 
    结贴数:1                  结贴分:30                 
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    这得用 firefox+firebug 或者 fidler 之类的http协议分析工具,看一下了。一般会检测header里面的 referer 是否正确
      

  3.   

    这得用 firefox+firebug 或者 fidler 之类的http协议分析工具,看一下了。一般会检测header里面的 referer 是否正确
      

  4.   

    header里面的referer正确呀...
    但还是返回不了结果...
      

  5.   

    没有user-agent的设定,给它弄成IE6或者7的还有就是确认一下,搜索parameter中GET和POST的信息。
    可以用live http headers监听一下正常搜索时的传输信息。
      

  6.   


    从你的代码中没有看到referer 
      

  7.   

    这是我的参数:
    NameValuePair[] data = {new NameValuePair("routeType", "single"),  //出发城市上海
          new NameValuePair("deptAirport", "SHA"), //到达城市北京
          new NameValuePair("deptDate","20080701"),//是否是单程
                            new NameValuePair("deptDateStr","2008-07-01"),//日                                                                       
                            new NameValuePair("arrAirport","PEK"),
                         new NameValuePair("returnDate","20080702"),//返回日期
                            new NameValuePair("returnDateStr","2008-07-02"),
                         new NameValuePair("x","36"),
                         new NameValuePair("y","8"),
                         new NameValuePair("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*"),
                         new NameValuePair("Accept-Language","zh-cn"),
                         new NameValuePair("Content-Type","application/x-www-form-urlencoded"),
                         new NameValuePair("Accept-Encoding","gzip, deflate"),
                         new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; TencentTraveler ; .NET CLR 2.0.50727; .NET CLR 1.1.4322)"),
                         new NameValuePair("Cookie","vgnvisitor=2w413000gHw0000Vi5xN89mTT0; JSESSIONID=SGgy7PpuaJy0wn2KfNQ1OXCol3MO26aU0s8S9Mi09kZ3vUWtMxMn!95700458508666893!-1407974901!8001!8002"),
                         new NameValuePair("Referer","http://www.ce-air.com/cea2/zh_CN/info/avail?sid=3005"),
                            };