gpt4 book ai didi

java - HttpURL连接问题

转载 作者:行者123 更新时间:2023-11-29 09:08:33 25 4
gpt4 key购买 nike

我正在尝试连接一个 http url。但它显示“已连接”异常。也请发现我的基本身份验证有任何问题。

  HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("POST");

BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = user + ":" + password;
String encodedAuthorization = enc.encode(userpassword.getBytes() );
conn.setRequestProperty("Authorization", "Basic "+ encodedAuthorization);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length",
String.valueOf(data.length()));
conn.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

最佳答案

使用这个

URLConnection conn = url.openConnection();
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
((HttpsURLConnection) conn).setRequestMethod(POST);
}

您可以从 here 获得帮助.

关于java - HttpURL连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13577656/

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