Java Android、代理和身份验证
作为为我的企业开发Android应用程序的一部分,我必须通过代理与认证服务器进行通信 以下是我编写的代码:Java Android、代理和身份验证,java,android,proxy,Java,Android,Proxy,作为为我的企业开发Android应用程序的一部分,我必须通过代理与认证服务器进行通信 以下是我编写的代码: final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_NAME, PROXY_PORT)));
final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_NAME, PROXY_PORT)));
Authenticator.setDefault(new NtlmAuthenticator(PROXY_USERNAME, PROXY_PASSWORD));
connection.setConnectTimeout(5000);
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setDoInput(true);
connection.setDoOutput(true);
final int retCode = connection.getResponseCode();
System.out.println("Return code : " + retCode);
和全班同学一起
private class NtlmAuthenticator extends Authenticator {
private final String username;
private final String password;
public NtlmAuthenticator(final String username, final String password) {
super();
this.username = username;
this.password = password;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("Get password auth");
return (new PasswordAuthentication(username, password.toCharArray()));
}
}
不幸的是,我仍然得到一个等于407的响应代码,并且我意识到我从未在getPasswordAuthentication方法中花费过时间
同样的程序可以很好地与Java程序(而不是Android)配合使用
我的Android版本是4.4.4,我见过它使用OkHttp进行查询。通过调试并输入Android的代码,我意识到该对象包含了我的代理配置,而不是我的验证器
你能帮我吗
谢谢您找到解决方案了吗?我面临着一个类似的问题