Java 使用Http身份验证的Https连接
我想在Android Studio中与Web服务建立HTTPS连接,但它与我编写的代码不兼容。我得到一个FileNotFound异常 我的URL正在浏览器中工作 响应代码是400 这是我的代码:Java 使用Http身份验证的Https连接,java,android,android-studio,https,http-authentication,Java,Android,Android Studio,Https,Http Authentication,我想在Android Studio中与Web服务建立HTTPS连接,但它与我编写的代码不兼容。我得到一个FileNotFound异常 我的URL正在浏览器中工作 响应代码是400 这是我的代码: protected Void doInBackground(String... params) { String https_url = "HERE MY HTTPS URL"; URL url; try {
protected Void doInBackground(String... params) {
String https_url = "HERE MY HTTPS URL";
URL url;
try {
url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
String userPassword = "USERNAME" + ":" + "PASSWORD";
String encoding = new String(Base64.encode(userPassword.getBytes(), Base64.DEFAULT));
con.setRequestProperty("Authorization", "Basic " + encoding);
con.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
con.setRequestProperty("Accept","*/*");
//dumpl all cert info
print_https_cert(con);
//dump all the content
print_content(con);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
有人能帮我吗?有几件事:1)你的URL是本地主机地址吗?如果是这样的话,可能会有一些跨域问题。2) 我对Base64.DEFAULT很好奇(说它可能需要关闭以确保URL安全,即使它是一个头值)3)你的“用户代理”头不是真正的Mozilla,对吗?你真的需要指定这个吗?4) 您的web服务接受哪些标头?通常,您需要准确地指定接受、使用、生成的内容,等等。我不使用Localhost。API使用摘要(示例2):