gpt4 book ai didi

java - 异常 : Already Connected, HttpsUrlConnection

转载 作者:行者123 更新时间:2023-12-01 04:45:41 27 4
gpt4 key购买 nike

我正在尝试对 URL 进行 https 连接。我收到此异常

Exception: Already Connected

我不确定我们如何通过 java 创建安全连接。谁能帮我这个?

这是我的代码。

public static String getCon() {
String result="",cookie="";
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
try {
URL url= new URL("https://jazz.net/");
HttpsURLConnection connection= (HttpsURLConnection) url.openConnection();
String cookieHeader = connection.getHeaderField("set-cookie");
if (cookieHeader != null) {
int index = cookieHeader.indexOf(";");
if (index >= 0)
cookie = cookieHeader.substring(0, index);
connection.setRequestProperty("Cookie", cookie);
}connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setFollowRedirects(false);

connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("User-Agent", "yes");
DataInputStream input = new DataInputStream( connection.getInputStream() );

StringBuffer buf= new StringBuffer();
// read in each character until end-of-stream is detected
for( int c = input.read(); c != -1; c = input.read() )
buf.append(c);
input.close();
result=buf.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
return result+" exception "+e.getMessage();
}


return result;
}

最佳答案

在建立连接之前您就获得了 Set-Cookie header 。没有任何意义。如果您希望在请求中设置该 header ,只需设置它即可。 getHeader() 从响应中获取 header 。

关于java - 异常 : Already Connected, HttpsUrlConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15899482/

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