gpt4 book ai didi

java - apache httpclient + ntlm 身份验证

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:52:18 25 4
gpt4 key购买 nike

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

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) - Authentication scheme ntlm not supported"

最佳答案

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

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

关于java - apache httpclient + ntlm 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com