Groovy中的HTTP身份验证?

时间:2016-03-03 21:00:46

标签: authentication groovy httpbuilder

def http = new HTTPBuilder(url)
http.auth.basic username, password

def data = new URL(url)

def response = data.getText()

def relativePaths = new XmlParser().parseText(response)

relativePaths.each {page ->

    def link = page.content.@href.first()

    if(link.contains(".txt")){
        println link
    }

}

您好,

当试图运行它时,我得到一个401.它似乎在这里:

def response = data.getText()

它实际上不再被认证(或根本没有)?在Ruby中,我会这样写:

(open(url, :http_basic_authentication => [username, password])
data = Nokogiri::XML(open(url, :http_basic_authentication => [username, password])))

在Groovy中会如何?我使用httpbuilder错了吗?非常感谢提前!

更新1:

应用更改后:

def http = new HTTPBuilder(url)
http.auth.basic username, password

http.get(path: url, contentType: ContentType.TEXT) { resp, reader ->

    def relativePaths = new XmlParser().parse(reader)

    relativePaths.each { page ->

        def link = page.content.@href.first()

        if (link.contains(".txt")) {
            println link
        }
    }
}

我得到了406:

回复代码:406;找到处理程序:org.codehaus.groovy.runtime.MethodClosure@5be46f9d 抓到:groovyx.net.http.HttpResponseException:不可接受 groovyx.net.http.HttpResponseException:Not Acceptable

更新2:

仍然无法正常工作。我将contentType更改为XML和实际路径的路径,但收到错误消息:

GET <URL>/<PATH>:(id:ContinuousIntegration_BuildTestDistribute)/artifacts/children/
Response code: 200; found handler: ArtifactDownloader$_run_closure1@5be46f9d
Parsing response as: application/xml
Could not find charset in response; using UTF-8
Parsed data to instance of: class groovy.util.slurpersupport.NodeChild
Caught: groovy.lang.MissingMethodException: No signature of method: groovy.util.XmlParser.parse() is applicable for argument types: (groovy.util.slurpersupport.NodeChild) values: []
Possible solutions: parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.lang.String), parse(org.xml.sax.InputSource), use([Ljava.lang.Object;)
groovy.lang.MissingMethodException: No signature of method: groovy.util.XmlParser.parse() is applicable for argument types: (groovy.util.slurpersupport.NodeChild) values: []
Possible solutions: parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.lang.String), parse(org.xml.sax.InputSource), use([Ljava.lang.Object;)
    at ArtifactDownloader$_run_closure1.doCall(ArtifactDownloader.groovy:14)
    at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:218)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160)
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
    at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:285)
    at groovyx.net.http.HTTPBuilder$get.call(Unknown Source)
    at ArtifactDownloader.run(ArtifactDownloader.groovy:12)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

仅供比较,Ruby脚本。也许httpbuilder不是我想要的?我实际上只想用它进行身份验证。

(open(url, :http_basic_authentication => [username, password])
data = Nokogiri::XML(open(url, :http_basic_authentication => [username, password])))

relativePaths = data.xpath("//content/@href")

relativePaths.each { |path| 
    if path.text.include? ".apk"
        puts "<URL>:<PORT>"+path.text
    end
}

1 个答案:

答案 0 :(得分:5)

你准备好面掌吗?

您创建了HTTPBuilder并且......从未使用过它。你完成了所有设置......

def http = new HTTPBuilder(url)
http.auth.basic(username, password)

...但随后使用新的URL来检索数据:

def data = new URL(url)
def response = data.getText()

解决方案

使用HTTPBuilder,如下所示:

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType

http.get(path : path, contentType : ContentType.TEXT) { resp, reader ->
    def relativePaths = new XmlParser().parse(reader)    
    ...
}

注意:我不确定get()方法是否在后台运行。

更新了解决方案

将内容类型设置为XML指示HTTPBuilder解析响应并将解析后的文档作为第二个参数提供。该文档表示为Node

http.get(path : path, contentType : ContentType.XML) { response, node ->
    def relativePaths = node    
    ...
}