groovy HTTP Builder不返回结果

时间:2012-02-17 16:20:13

标签: groovy httpbuilder

我在groovy中有以下代码

HTTPBuilder http = new HTTPBuilder("https://ronna-afghan.harmonieweb.org/_layouts/searchrss.aspx")



        http.request(Method.GET, groovyx.net.http.ContentType.XML) {

            // set username and password for basic authentication
            // set username and password for basic auth
            //http.auth.basic(ConfigurationHolder.config.passportService.userName,
            //        ConfigurationHolder.config.passportService.password)
            headers.'User-Agent' = 'Mozilla/5.0'

            uri.query = [k:'execution']

            // response handler for a success response code:
            response.success = {resp, xml ->
                println resp.statusLine



                log.debug "response status: ${resp.statusLine}"
                log.debug xml.toString()

            }

            // handler for any failure status code:
            response.failure = {resp ->
                log.error " ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
            }


        }

当我运行代码时,它没有给我rss feed我想要得到

当我在java中有相同的代码时

try {
            // Create a URLConnection object for a URL
            URL oracle = new URL(
                    "https://ronna-afghan.harmonieweb.org/_layouts/srchrss.aspx?k=execution&count=1&format=rss");

            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                in.close();
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

它返回xml Rss。我无法弄清楚问题可能是什么。在groovy代码中,一切看起来都没问题,Http返回代码也是200。

1 个答案:

答案 0 :(得分:3)

您在Java中描述的代码相当于Groovy中的以下代码:

def oracle = "https://ronna-afghan.harmonieweb.org/_layouts/srchrss.aspx?k=execution&count=1&format=rss".toURL().text