Iam尝试对https URL进行身份验证,但Iam出现异常。下面是代码。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class Authenticate {
/**
* @param args
*/
public void authenticateUrl() {
HostnameVerifier hv = new HostnameVerifier() {
@Override
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName
+ " vs. " + session.getPeerHost());
return true;
}
};
// Now you are telling the JRE to trust any https server.
// If you know the URL that you are connecting to then this should
// not be a problem
try {
trustAllHttpsCertificates();
} catch (Exception e) {
System.out.println("Trustall" + e.getStackTrace());
}
HttpsURLConnection.setDefaultHostnameVerifier(hv);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try {
URL url = new URL(
"www.stackoverflow.com");
// Popup Window to request username/password password
// MyAuthenticator ma = new MyAuthenticator();
String userPassword = "user" + ":" + "pass";
// Encode String
String encoding = URLEncoder.encode(userPassword, "UTF-8");
// or
// String encoding = Base64Converter.encode
// (userPassword.getBytes());
// Need to work with URLConnection to set request property
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", "UTF-8" + encoding);
InputStream content = (InputStream) uc.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(
content));
String line;
while ((line = in.readLine()) != null) {
pw.println(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
pw.println("Invalid URL");
} catch (IOException e) {
e.printStackTrace();
pw.println("Error reading URL");
} catch (Exception e) {
e.printStackTrace();
}
sw.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Authenticate au = new Authenticate();
au.authenticateUrl();
}
// Just add these two functions in your program
public static class TempTrustedManager implements
javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
private static void trustAllHttpsCertificates() throws Exception {
// Create a trust manager that does not validate certificate chains:
javax.net.ssl.TrustManager[] trustAllCerts =
new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new TempTrustedManager();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc =
javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(
sc.getSocketFactory());
}
}
例外情况:
java.io.IOException: Server returned HTTP response code: 401 for URL:
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at Authenticate.authenticateUrl(Authenticate.java:62)
at Authenticate.main(Authenticate.java:84)
请任何人建议如何解决此问题。
401错误代码表示“未经授权”。我相信您的代码无法正确编码Authentication标头。假设服务器需要 基本访问身份验证, 则代码应如下所示:
String credentials = "ptt" + ":" + "ptt123";
String encoding = Base64Converter.encode(credentials.getBytes("UTF-8"));
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", String.format("Basic %s", encoding));
RFC
2617
中提供了HTTP基本和摘要身份验证方案的全面描述
问题内容: 我的代码中包含基于HTML的查询,并且从服务器收到505响应后,似乎会引起一种特定的查询。我与其他似乎也有类似问题的人一起查看了505响应。显然505代表HTTP版本不匹配,但是当我将相同的查询URL复制到任何浏览器(尝试过firefox,seamonkey和Opera)时,似乎没有问题。我读过的一篇文章建议浏览器可能会自动处理版本不匹配的问题。 我试图通过使用Opera附带的漂亮的开
问题内容: 我正在用Java面对这个问题。我想从URL获取一些HTML信息。该代码已经运行了很长时间,但是突然,它停止了工作。 当我使用浏览器访问此URL时,它毫无问题地打开。 代码: 例外: 怎么了?主人阻止了我吗? 问题答案: HTTP状态代码500通常表示Web服务器代码已崩溃。您需要预先确定状态代码,如果有错误,请阅读。它可能即包含有关问题的信息。 如果主机阻止了您,您宁愿获得4nn状态码
问题内容: 我试图从桌面应用程序连接到URL,但出现问题标题中指示的错误,但是当我尝试从servlet连接到同一URL时,一切正常。当我从浏览器加载URL时,一切正常。我在servlet中使用相同的代码。该代码在库中,当它不起作用时,我将代码拉到当前项目中的类中,但它不起作用。 网址https://graph.facebook.com/me。 代码片段。 我在这里有些困惑,是否存在某种不是普通桌面
问题内容: 我想从URL打开一个链接:“ http://www.kohls.com/search.jsp?search=jacket&submit- search=web-regular ”,有时我得到: java.io.IOException:服务器返回URL的HTTP响应代码:403。但是使用浏览器打开网址是可以的。以下是我的代码的一部分: 错误详情 线程“主”中的异常java.io.IOEx
这个错误的原因是什么?当我直接从浏览器调用URL时,它的工作非常完美。
java.io.ioException:服务器返回HTTP响应代码:406 for url:https://aa09616289afe6a25ea1f7f6fbf12073:shppa_f5fd47a653d49d886436578c54187@mfaisal1521.myshopify.com/admin/api/2020-04/products.json at sun.net.www.prot