java apache httpclient + ntlm 认证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10579279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:39:24  来源:igfitidea点击:

apache httpclient + ntlm Authentication

javafile-uploadntlmapache-httpclient-4.x

提问by user732362

I have a client to upload a file to a server over https post. It uses proxy and this is my code

我有一个客户端通过 https 帖子将文件上传到服务器。它使用代理,这是我的代码

public void upload() throws Exception {

    //create default client
    DefaultHttpClient client = new DefaultHttpClient();

    //set proxy authentication if specified
    if (proxy.equals("yes") && proxyAuth.equals("yes")){
    client.getCredentialsProvider().setCredentials(
            new AuthScope(address, port),
            new UsernamePasswordCredentials(proxyUsername, proxyPassword));
    }

    //set proxy if specified
    if (proxy.equals("yes")){
        HttpHost proxy = new HttpHost(address, port);
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
    }

    HttpPost post = new HttpPost(url);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    File dir = new File(inputFilePath);
    File[] fileArray = dir.listFiles(); 
    File file = fileArray[0];

    FileBody uploadFilePart = new FileBody(file);

    entity.addPart("file", uploadFilePart);
    entity.addPart("username", new StringBody(username));
    entity.addPart("password", new StringBody(password));

    post.setEntity(entity);

    //execute post and get response
    String response = EntityUtils.toString(client.execute(post).getEntity(), "UTF-8");

    client.getConnectionManager().shutdown();

    log4j.info(response);

    if(!response.substring(0, 3).equalsIgnoreCase("200")){
        Exception e = new Exception("An error has occurred server side: ");
        throw e;
    }
}

Now the problem is this works perfectly sometimes and sometimes i get the below error.

现在的问题是这有时完美无缺,有时我会收到以下错误。

org.apache.http.impl.client.AbstractAuthenticationHandler.selectScheme(AbstractAuthenticationHandler.java:149) - Authentication scheme ntlm not supported"

org.apache.http.impl.client.AbstractAuthenticationHandler.selectScheme(AbstractAuthenticationHandler.java:149) - 不支持身份验证方案 ntlm”

回答by Wim Deblauwe

You need to register the NTLM handler as explained in http://hc.apache.org/httpcomponents-client-ga/ntlm.html:

您需要按照http://hc.apache.org/httpcomponents-client-ga/ntlm.html 中的说明注册 NTLM 处理程序:

client.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());

回答by user2626270

Try to - Instead of this: new UsernamePasswordCredentials(proxyUsername, proxyPassword)

尝试 - 而不是这个: new UsernamePasswordCredentials(proxyUsername, proxyPassword)

use this: new NTCredentials(proxyUsername, proxyPassword, "localhostname", "domain")

使用这个:新的 NTCredentials(proxyUsername, proxyPassword, "localhostname", "domain")