java里怎样在客户端获取response的Cookie

URL geturl = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection)geturl.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");

String cookie = conn.getHeaderField("set-cookie");   

System.out.println(cookie); 



  1.         String key = null;   
  2.         for (int i = 1; (key = http.getHeaderFieldKey(i)) != null; i++) {   
  3.             if (key.equalsIgnoreCase("set-cookie")) {   
  4.                 String cookie = null;   
  5.                 cookie = http.getHeaderField(i);   
  6.                 int i1=cookie.indexOf("=");   
  7.                 int i2=cookie.indexOf(";");   
  8.                 if(i1!=-1&&i2!=-1){   
  9.                     String _value=cookie.substring(i1+1, i2);   
  10.                     String _key=cookie.substring(0, i1);   
  11.                     cookieMap.put(_key, _value);   
  12.                 }   
  13.             }   
  14.         }  

 

 

你可能感兴趣的:(java里怎样在客户端获取response的Cookie)