English 中文(简体)
co在 Java的HttpURLConnection中什么?
原标题:When is the cookie set in HttpURLConnection with Java?

审视这些法典:

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();
最佳回答

如果尚未建立这种联系,连接法将打开与本《欧洲统一法》参考资源的联系。 获得投入、获得反应或恢复使用的方法将产生与服务器的通信联系

问题回答

因此,一般而言,只要采用这种HttpURLConnection级的方法,即恢复反应(如植被指数/植被反应/植被焦土)即为多余,就会实际进行暗中的联系。 什么时候?

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签