【问题标题】:groovy HTTP Builder not returning resultsgroovy HTTP Builder 不返回结果
【发布时间】:2012-02-17 16:20:13
【问题描述】:

我在 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 提要

当我在 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。

【问题讨论】:

    标签: groovy httpbuilder


    【解决方案1】:

    您在 Java 中描述的代码等同于 Groovy 中的以下代码:

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

    【讨论】:

    • 有道理。它现在工作。我从配置文件中读取了 URL,其中有一个错字。因此获取失败。两个代码都返回正确的结果。
    • 您认为所提供的答案是给定问题详细信息的问题可接受的答案吗? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    相关资源
    最近更新 更多