gpt4 book ai didi

java - java访问https站点的方法

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:56:07 25 4
gpt4 key购买 nike

我必须通过我的 java 代码访问 https 站点。但它返回 401 响应。我在下面包含了我的代码。

try {        
URL u = new URL(url);
HttpURLConnection http = (HttpURLConnection)u.openConnection();
http.setAllowUserInteraction(true);
http.connect();

String userpassword = "HP:M0lveau";
byte[] encoded = Base64.encodeBase64(userpassword.getBytes());
String encodedAuthorization = new String(encoded);

http.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
InputStream is = http.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();

} catch (IOException ioe) {
logger.debug("fetchDataFromServer:IOException");
return null;
}

请尽早提供帮助..提前致谢....

最佳答案

尝试在调用 connect() 之前设置授权 header

关于java - java访问https站点的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4755647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com