这个星期的工作要做个meta Search,我用HttpClient写了个简单的demo,但是一个网站需要post,但是其中一个parameter在post中文的时候老是得不到结果,不知道怎么回事(英文是可以的)

解决方案 »

  1.   

    UrlEncoder好象也不怎么行啊,是不是我的代码有问题,下面是部分代码
    HttpClient httpClient = new HttpClient();
    String url = "http://book.du8.com/book/BookSearch.do";
    String keyword = "中国";
              keyword = java.net.URLEncoder.encode(keyword,"GBK");
    System.out.println(keyword);
    PostMethod method = new PostMethod(url);

        //填入各个表单域的值
    /*NameValuePair[] data = {new NameValuePair("queryType", "bkname"),
      new NameValuePair("keyword", "tcp"),new NameValuePair("secondQuery","true"),
      new NameValuePair("operator","and"),new NameValuePair("ljjs.x","57"),new NameValuePair("ljjs.y","7")};
    */
    NameValuePair[] data = {new NameValuePair("selectSearch", "bookName")};
    method.setQueryString(new NameValuePair[]{new NameValuePair("keyword", keyword)});
        //将表单的值放入postMethod中
    method.setRequestBody(data);
    method.setFollowRedirects(false);
        //执行method
    StringBuffer resultBuffer = new StringBuffer();
    try {
    httpClient.executeMethod(method);
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    method.getResponseBodyAsStream(),
    method.getResponseCharSet()));
    String inputLine = null;
    while ((inputLine = in.readLine()) != null) {
    //inputLine = ConverterStringCode(inputLine,method.getResponseCharSet(),"gb2312");
    resultBuffer.append(inputLine);
    resultBuffer.append("\n");
    }
    in.close();
    System.out.println(resultBuffer.toString());
    FileWriter write = new FileWriter("d:\\test\\test.txt");
    write.write(resultBuffer.toString());
    } catch (HttpException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }