簡體   English   中英

Java httpget似乎可以工作,但是遇到401錯誤

[英]Java httpget seems to work but encountering 401 error

由於到目前為止的反饋,已經重寫了...

我有一個Java方法來嘗試通過硒腳本中的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(). 設置正確,或者如果我在這里缺少任何東西...

任何進一步的幫助,不勝感激!

您可以通過兩種不同的方式設置BASIC身份驗證憑據:

設置適當的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版本)

HttpClient hc = new DefaultHttpClient();

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

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

hc.execute(get);

我終於設法使我的代碼正常工作,最終版本如上所示。 我犯了一些明顯的錯誤,例如我的域仍然設置為示例“ microsoft.com”,而實際上我拼錯了密碼,除了它現在確實可以滿足我的要求! :)

@vzamanillo感謝您的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM