我有一个肥皂终点,用户和密码
user =“Org API / uAPI529457959-e784379”
密码=“t61 / B = 09 {v”
HttpClient Imports
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
我在请求标头中使用了基本身份验证
String userPassword = user + ":" + password;
setRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(userPassword.getBytes())));
工作正常。
但是当我试图在end-pont url中传递用户和密码时。 http://user:password@example.com
提供以下日志。
2016年12月2日下午12:51:44 org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme INFO:12月02日选择的基本认证方案, 2016 12:51:44 org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge INFO:没有可用于BASIC的凭据 “AXIS'@example.com:443
我在传递网址之前编码了用户和密码。我没有得到任何线索如何解决这个问题。请求帮助。
答案 0 :(得分:0)
您正在使用Apache HttpClient,因此您应该以这种方式做得更好:
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "userpass");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
HttpResponse response = client.execute(new HttpGet("http://domain.com"));
检查这个很好的例子:HttpClient Basic Authentication