3

look at these code:

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();
    }

please notice these three lines:

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("\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();
Jonas
  • 97,987
  • 90
  • 271
  • 355
CaiNiaoCoder
  • 3,089
  • 9
  • 49
  • 78

2 Answers2

3

The method connect() will Opens a communications link to the resource referenced by this URL, if such a connection has not already been established. getInputStream(), getResponseCode(), or getResponseMessage() methods will create a communication link with server

vinoth
  • 481
  • 3
  • 7
  • 14
  • but how do you explain that. In my second example, I did not invoke the open() method at all , but I can get response from server too. – CaiNiaoCoder Nov 23 '11 at 09:25
  • @CaiNiaoCoder Look at this ans http://stackoverflow.com/questions/2151359/java-httpurlconnection-doesnt-connect-when-i-call-connect . Thanks for the question – vinoth Nov 23 '11 at 10:19
1

So in general speaking, #connect() is in fact superfluous, whenever a method of this HttpURLConnection class, that is retrieving a response info (such as #getInputStream()/getResponseCode()/getHeaderFields()/etc.), is invoked, an implicit connect would actually be done. Am I right?

My understanding is based on the below discussion: Using java.net.URLConnection to fire and handle HTTP requests

Community
  • 1
  • 1
Roy Ling
  • 1,522
  • 14
  • 28