审视这些法典:
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("
[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#openConnection and URL#connect, which method send the request to server? if the answer is openConnection method, how can a cookie be set after a request already been sent? if the answer is connect method, you guess what ? these code below works too, I can get response from server :
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("
[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();