Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http groovy-下载带有身份验证的文件_Http_Authentication_Groovy_Httprequest_Basic Authentication - Fatal编程技术网

Http groovy-下载带有身份验证的文件

Http groovy-下载带有身份验证的文件,http,authentication,groovy,httprequest,basic-authentication,Http,Authentication,Groovy,Httprequest,Basic Authentication,我需要使用Groovy下载一个使用基本身份验证(这种身份验证会提示浏览器向您询问域\用户名和密码)的文本文件。我希望避免使用额外的库,Groovy中没有任何东西可以做到这一点吗 我目前的代码是: new File("test.txt").withOutputStream { out -> def url = new URL(myurl).openConnection() def remoteAuth = "Basic " + "myusername:mypassword"

我需要使用Groovy下载一个使用基本身份验证(这种身份验证会提示浏览器向您询问域\用户名和密码)的文本文件。我希望避免使用额外的库,Groovy中没有任何东西可以做到这一点吗

我目前的代码是:

new File("test.txt").withOutputStream { out ->
    def url = new URL(myurl).openConnection()

    def remoteAuth = "Basic " + "myusername:mypassword".bytes.encodeBase64()
    url.setRequestProperty("Authorization", remoteAuth);
    out << url.inputStream
}
新文件(“test.txt”).withOutputStream{out->
def url=newurl(myurl).openConnection()
def remoteAuth=“Basic”+“myusername:mypassword”.bytes.encodeBase64()
setRequestProperty(“授权”,remoteAuth);

outGroovy使用
java.net.Authenticator
API。您可以使用提供默认的
Authenticator
。可以找到基本用法的示例


Groovy使用
java.net.Authenticator
API。您可以使用提供默认的
Authenticator
。可以找到基本用法的示例


非常感谢,这立即生效!我从代码中删除了两行验证代码并添加了您的。非常感谢,这立即生效!我从代码中删除了两行验证代码并添加了您的。
Authenticator.setDefault (new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication ("username", "password".toCharArray());
    }
});