我试着用HttpsURLConnection
打我的服务器,得到响应代码401-需要验证。尝试了以下操作:
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication ("myuser", "!mypassword".toCharArray());
}
});
收到响应代码400-请求错误。
基于一些stackoverflow研究,我还尝试使用:
String authorizationString = "Basic " + Base64.encodeToString(("username" + ":" + "password").getBytes(), Base64.DEFAULT);
conn.setRequestProperty("Authorization", authorizationString);
同样的问题,代码400。在这篇文章中,我也信任所有使用@chrispix方法的主机:
Trust Anchor not found for Android SSL Connection
不确定这是否会干扰,但这是一个原型和网站证书是无效的,所以我需要绕过检查。
连接是用这些属性设置的,不确定是否有任何错误或丢失:
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.addRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoInput(true);
谢谢!
最佳答案
使用HttpClient
怎么样?
HttpClient httpClient= new DefaultHttpClient();
HttpGet httpGet= new HttpGet(yourUrl);
String authString = (yourUserName+":"+yourPassword);
httpGet.addHeader("Authorization", "Basic " +
Base64.encodeToString(authString.getBytes(),Base64.DEFAULT));
HttpResponse response = httpClient.execute(httpGet);
似乎你想通过目前的证书检查,这个相关的链接可以帮助你
Trusting all certificates using HttpClient over HTTPS
关于android - 身份验证器响应码400,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24421825/