gpt4 book ai didi

java - 什么时候用 Java 在 HttpURLConnection 中设置 cookie?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:24:14 30 4
gpt4 key购买 nike

看看这些代码:

public static String get(String url, Properties parameters) throws MalformedURLException, IOException{
url = buldGetUrl(url, parameters);
if(DEBUG) System.out.println("[HTTP GET REQUEST]");
if(DEBUG) System.out.println(" URL: " + url);
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("Cookie","JSESSIONID=" + J_SESSION_ID);
conn.connect();

if(DEBUG) System.out.println("\n[HTTP GET RESPONSE]");
if(DEBUG) System.out.println("==== Response Headers =====");
String serverCookies = conn.getHeaderField("Set-Cookie");
if(serverCookies != null){
String[] cookies = serverCookies.split(";");
for(String s : cookies){
s = s.trim();
if(s.split("=")[0].equals("JSESSIONID")){
J_SESSION_ID = s.split("=")[1];
if(DEBUG) System.out.println(" Session ID Found: " + J_SESSION_ID);
break;
}
}
}
if(DEBUG){
for(String s : conn.getHeaderFields().keySet()){
if(s == null)
System.out.println(" " + conn.getHeaderField(s));
else
System.out.println(" " + s + "=" + conn.getHeaderField(s));
}
}

if(DEBUG) System.out.println("==== Response Content =====");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str = null;
StringBuilder sb = new StringBuilder();
while ((str = br.readLine()) != null) {
sb.append(str + System.getProperty("line.separator"));
if(DEBUG) System.out.println(str);

}
br.close();
return sb.toString();
}

请注意这三行:

HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();    
conn.setRequestProperty("Cookie","JSESSIONID=" + J_SESSION_ID);
conn.connect();

URL#openConnectionURL#connect,哪个方法向服务器发送请求?如果答案是openConnection 方法,如何在已发送请求后设置cookie?如果答案是connect 方法,您猜怎么着?下面这些代码也有效,我可以从服务器得到响应:

url = buldGetUrl(url, parameters);
if(DEBUG) System.out.println("[HTTP GET REQUEST]");
if(DEBUG) System.out.println(" URL: " + url);
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

if(DEBUG) System.out.println("\n[HTTP GET RESPONSE]");
if(DEBUG) System.out.println("==== Response Headers =====");
String serverCookies = conn.getHeaderField("Set-Cookie");
if(serverCookies != null){
String[] cookies = serverCookies.split(";");
for(String s : cookies){
s = s.trim();
if(s.split("=")[0].equals("JSESSIONID")){
J_SESSION_ID = s.split("=")[1];
if(DEBUG) System.out.println(" Session ID Found: " + J_SESSION_ID);
break;
}
}
}
if(DEBUG){
for(String s : conn.getHeaderFields().keySet()){
if(s == null)
System.out.println(" " + conn.getHeaderField(s));
else
System.out.println(" " + s + "=" + conn.getHeaderField(s));
}
}

if(DEBUG) System.out.println("==== Response Content =====");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str = null;
StringBuilder sb = new StringBuilder();
while ((str = br.readLine()) != null) {
sb.append(str + System.getProperty("line.separator"));
if(DEBUG) System.out.println(str);

}
br.close();
return sb.toString();

最佳答案

方法 connect() 将打开到此 URL 引用的资源的通信链接,如果这样的连接尚未建立的话。 getInputStream()、getResponseCode() 或 getResponseMessage() 方法将创建与服务器的通信链接

关于java - 什么时候用 Java 在 HttpURLConnection 中设置 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8239453/

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