Groovy中的HTTP身份验证?

dan*_*ski 1 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
    }

}
Run Code Online (Sandbox Code Playgroud)

嗨,

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

def response = data.getText()
Run Code Online (Sandbox Code Playgroud)

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

(open(url, :http_basic_authentication => [username, password])
data = Nokogiri::XML(open(url, :http_basic_authentication => [username, password])))
Run Code Online (Sandbox Code Playgroud)

在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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到406:

回复代码:406; 找到处理程序:org.codehaus.groovy.runtime.MethodClosure@5be46f9d抓到:groovyx.net.http.HttpResponseException:Not Acceptable 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)
Run Code Online (Sandbox Code Playgroud)

仅供比较,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
}
Run Code Online (Sandbox Code Playgroud)

Emm*_*osa 5

你准备好面掌吗?

你创造了一个HTTPBuilder......从未使用它.你把它全部设置好了......

def http = new HTTPBuilder(url)
http.auth.basic(username, password)
Run Code Online (Sandbox Code Playgroud)

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

def data = new URL(url)
def response = data.getText()
Run Code Online (Sandbox Code Playgroud)

一个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)    
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

更新解决方案

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

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