Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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
Java httpget似乎可以工作,但遇到401错误_Java_Http_Authentication_Http Get - Fatal编程技术网

Java httpget似乎可以工作,但遇到401错误

Java httpget似乎可以工作,但遇到401错误,java,http,authentication,http-get,Java,Http,Authentication,Http Get,由于到目前为止的反馈,已重新编写 我有一个Java方法可以在selenium脚本中通过httpget下载一个文件,看起来是这样的 private String downloader(WebElement element, String attribute, String filePath) throws IOException, NullPointerException, URISyntaxException { String fileToDownloadLocat

由于到目前为止的反馈,已重新编写

我有一个Java方法可以在selenium脚本中通过httpget下载一个文件,看起来是这样的

 private String downloader(WebElement element, String attribute, String filePath) throws IOException, NullPointerException, URISyntaxException {      
        String fileToDownloadLocation = element.getAttribute(attribute);
        fileToDownloadLocation = fileToDownloadLocation.replace("\\", "%5C");
        URL fileToDownload = new URL(fileToDownloadLocation);
        URI FileDL = new URI(fileToDownload.toString());


        File downloadedFile = new File(this.localDownloadPath + filePath);
        if (downloadedFile.canWrite() == false) downloadedFile.setWritable(true);

        CloseableHttpClient httpclient = HttpClients.createDefault();

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY,new NTCredentials("useraccount", "testpassword", "machinename", "mydomain"));

        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);

        HttpGet httpget = new HttpGet(FileDL);        

        CloseableHttpResponse response = httpclient.execute(httpget, context);

        this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode();

        FileUtils.copyInputStreamToFile(response.getEntity().getContent(), downloadedFile);
        response.getEntity().getContent().close();

        String downloadedFileAbsolutePath = downloadedFile.getAbsolutePath();
        return downloadedFileAbsolutePath;
}
当我在常规链接上使用它时(例如,从bbc下载文件),一切正常。问题是我使用它的系统是一个内部系统,它使用windows身份验证来确定是否允许访问。当我试图在这个内部系统上使用它时,我的Http响应结果是:-

HTTP/1.1 401 Unauthorized [Cache-Control: no-cache, no-store, must-revalidate, Pragma: no-cache, Content-Type: text/html, Expires: -1, Server: Microsoft-IIS/7.5, WWW-Authenticate: Negotiate, WWW-Authenticate: NTLM, X-Powered-By: ASP.NET, X-UA-Compatible: IE=edge, Date: Mon, 17 Mar 2014 14:52:43 GMT, Content-Length: 1293]
如果我调试它并获得它使用的URL,我可以手动将它粘贴到浏览器中,它工作正常

我还注意到,在调试过程中,httpRequestParameters似乎根本没有任何值(显示为null),因此我猜我仍然没有正确设置帐户参数

我会诚实地说,我真的不是100%了解这一切的正确性,我正在尝试将东西粘贴在一起,让它发挥作用,但我想知道我的
((AbstractHttpClient)client.getCredentialsProvider()。
部分是否设置正确,或者我是否遗漏了一些东西


非常感谢任何进一步的帮助

您可以通过两种不同的方式设置基本身份验证凭据:

设置已批准的HTTP标头:

HttpClient hc = new DefaultHttpClient();

HttpGet get = new HttpGet("http://...");

String basic_auth = new String(Base64.encodeBase64((username + ":" + password).getBytes()));
get.addHeader("Authorization", "Basic " + basic_auth);
或者使用
CredentialsProvider
(取决于Apache HttpComponents版本)


我终于设法让我的代码正常工作了,最终版本如上图所示。我犯了一些明显的错误,比如我的域名仍然设置为示例“microsoft.com”,我实际上拼写错误了我的密码,除此之外,它现在正是我所需要的


@vzamanillo谢谢你的帮助

似乎您需要凭据才能真正下载文件。您可以在浏览器中查看,而无需任何凭据。是的,查看效果很好,如果我手动转到链接,我可以查看它,并通过通常的过程下载它,而不需要任何问题或凭据,但我猜尝试以这种方式下载文件需要凭据,我不知道如何指定这些…:我可能会联系运行你想下载的网站的人。该网站是内部的,我知道使用什么用户名/密码才能访问它,我只是不知道如何设置它们:由于回复延迟,其他事情接手了。当我尝试实现第二个示例时,我在
getCredentialsProvider
行中遇到了一个问题,Eclipse告诉我
类型HttpClient的getCredentialsProvider()方法未定义,并且我需要向HCC添加强制转换您使用的Eclipse的哪个版本?Indigo service release 1。如果我添加强制转换,看起来似乎工作正常,但是当我运行execute时,我会收到一个错误,读取
org.apache.http.client.protocol.RequestTargetAuthentication进程严重:身份验证错误:未提供有效凭据(机制级别:未提供有效凭据)(机制级别:找不到任何Kerberos tgt))
需要使用哪种身份验证,HTTP BASIC还是什么?
HttpClient hc = new DefaultHttpClient();

hc.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));

HttpGet get = new HttpGet ("http://...");       

hc.execute(get);