CookieUtil.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:3k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.util.http;
  2. import net.javacoding.jspider.api.model.Cookie;
  3. import java.net.URLConnection;
  4. import java.util.ArrayList;
  5. import java.util.StringTokenizer;
  6. /**
  7.  *
  8.  * $Id: CookieUtil.java,v 1.8 2003/04/08 18:44:15 vanrogu Exp $
  9.  *
  10.  * @author G黱ther Van Roey
  11.  */
  12. public class CookieUtil {
  13.     public static final String HEADER_SET_COOKIE = "Set-Cookie";
  14.     public Cookie[] getCookies(URLConnection connection) {
  15.         String[] cookieStrings = getCookieStrings(connection);
  16.         return getCookies(cookieStrings);
  17.     }
  18.     protected String[] getCookieStrings(URLConnection connection) {
  19.         ArrayList arrayList = new ArrayList();
  20.         String headerKey = null;
  21.         String headerValue = null;
  22.         int i = 0;
  23.         do {
  24.             headerKey = connection.getHeaderFieldKey(i);
  25.             // Must be read here because the HTTP header's key is null, so loop would end otherwise
  26.             headerValue = connection.getHeaderField(i);
  27.             if (HEADER_SET_COOKIE.equalsIgnoreCase(headerKey)) {
  28.                 if (headerValue != null) {
  29.                     arrayList.add(headerValue.trim());
  30.                 } else {
  31.                     arrayList.add(headerValue);
  32.                 }
  33.             }
  34.             i++;
  35.         } while (headerKey != null || headerValue != null);
  36.         return (String[]) arrayList.toArray(new String[arrayList.size()]);
  37.     }
  38.     protected Cookie[] getCookies(String[] cookieString) {
  39.         Cookie[] cookies = new Cookie[cookieString.length];
  40.         int used = 0;
  41.         for (int i = 0; i < cookieString.length; i++) {
  42.             String s = cookieString[i];
  43.             Cookie cookie = getCookie(s);
  44.             if (cookie != null) {
  45.                 cookies[used++] = cookie;
  46.             }
  47.         }
  48.         Cookie[] retVal = new Cookie[used];
  49.         System.arraycopy(cookies, 0, retVal, 0, used);
  50.         return retVal;
  51.     }
  52.     public Cookie getCookie(String cookieString) {
  53.         Cookie cookie = null;
  54.         if (cookieString != null && !cookieString.trim().equals("")) {
  55.             StringTokenizer st = new StringTokenizer(cookieString, ";");
  56.             String name = "";
  57.             String value = "";
  58.             String domain = "";
  59.             String path = "";
  60.             String expires = "";
  61.             // First part of a cookie string is name=value; ...
  62.             while (st.hasMoreTokens()) {
  63.                 String token = st.nextToken();
  64.                 StringTokenizer st2 = new StringTokenizer(token, "=");
  65.                 if (st2.hasMoreTokens()) {
  66.                     String propName = st2.nextToken().trim();
  67.                     if (st2.hasMoreTokens()) {
  68.                         String propValue = st2.nextToken().trim();
  69.                         if ("value".equalsIgnoreCase(propName)) {
  70.                             value = propValue;
  71.                         } else if ("domain".equalsIgnoreCase(propName)) {
  72.                             domain = propValue;
  73.                         } else if ("path".equalsIgnoreCase(propName)) {
  74.                             path = propValue;
  75.                         } else if ("expires".equalsIgnoreCase(propName)) {
  76.                             expires = propValue;
  77.                         } else {
  78.                             name = propName;
  79.                             value = propValue;
  80.                         }
  81.                     }
  82.                 }
  83.             }
  84.             cookie = new Cookie(name, value, domain, path, expires);
  85.         }
  86.         return cookie;
  87.     }
  88. }