首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >apache httpclient + ntlm身份验证

apache httpclient + ntlm身份验证
EN

Stack Overflow用户
提问于 2012-05-14 07:43:57
回答 2查看 16.5K关注 0票数 3

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

代码语言:javascript
运行
复制
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;
    }
}

现在的问题是,这是完美的工作有时,有时我得到以下错误。

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

EN

回答 2

Stack Overflow用户

发布于 2012-10-04 11:59:12

您需要注册NTLM处理程序,如http://hc.apache.org/httpcomponents-client-ga/ntlm.html中所解释的那样

代码语言:javascript
运行
复制
client.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
票数 4
EN

Stack Overflow用户

发布于 2015-10-20 16:04:04

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10579279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档