diff --git a/android-jdk6-redkale/src/com/wentch/redkale/net/client/HttpClient.java b/android-jdk6-redkale/src/com/wentch/redkale/net/client/HttpClient.java index eb3d07841..88e9fc2a5 100644 --- a/android-jdk6-redkale/src/com/wentch/redkale/net/client/HttpClient.java +++ b/android-jdk6-redkale/src/com/wentch/redkale/net/client/HttpClient.java @@ -6,25 +6,84 @@ package com.wentch.redkale.net.client; import java.net.*; +import java.util.*; /** * * @author zhangjx */ -public class HttpClient { +public final class HttpClient { + + private List cookies; + + private Map headers; + + private Map params; + + private final HttpFactory factory; private final URL url; - public HttpClient(URL url) { + HttpClient(HttpFactory factory, URL url) { + this.factory = factory; this.url = url; } public HttpClient setTimeoutListener(Runnable runner) { return this; } - + + public Map getHeaders() { + return headers == null ? null : new HashMap(headers); + } + + public HttpClient addHeader(String name, String value) { + if (headers == null) this.headers = new HashMap(); + headers.put(name, value); + return this; + } + + public HttpClient removeHeader(String name) { + if (this.headers == null) return this; + headers.remove(name); + return this; + } + + public List getCookies() { + return cookies == null ? null : new ArrayList(cookies); + } + + public HttpClient addCookie(HttpCookie cookie) { + if (cookies == null) this.cookies = new ArrayList(); + if (cookie != null) cookies.add(cookie); + return this; + } + + public HttpClient addCookie(HttpCookie... cookies) { + if (cookies == null) this.cookies = new ArrayList(); + if (cookies != null) { + for (HttpCookie c : cookies) { + if (c != null) this.cookies.add(c); + } + } + return this; + } + + public HttpClient removeCookie(String name) { + if (this.cookies == null) return this; + HttpCookie cookie = null; + for (HttpCookie c : cookies) { + if (c.getName().equals(name)) { + cookie = c; + break; + } + } + cookies.remove(cookie); + return this; + } + public static void main(String[] args) throws Exception { - URL url = new URL("https://www.3wyc.com"); - System.out.println(url.openConnection().getClass()); + URL url = new URL("https://www.wentch.com"); + System.out.println(url.openConnection().getClass()); } } diff --git a/android-jdk6-redkale/src/com/wentch/redkale/net/client/HttpFactory.java b/android-jdk6-redkale/src/com/wentch/redkale/net/client/HttpFactory.java new file mode 100644 index 000000000..65d45140b --- /dev/null +++ b/android-jdk6-redkale/src/com/wentch/redkale/net/client/HttpFactory.java @@ -0,0 +1,85 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.wentch.redkale.net.client; + +import java.net.*; +import java.util.*; +import java.util.concurrent.*; + +/** + * + * @author zhangjx + */ +public class HttpFactory { + + private static final HttpFactory instance = new HttpFactory(null); + + final HttpFactory parent; + + final List defaultCookies = new CopyOnWriteArrayList(); + + final Map defaultHeaders = new ConcurrentHashMap(); + + private HttpFactory(HttpFactory parent) { + this.parent = parent; + } + + public HttpFactory parent() { + return this.parent; + } + + public static HttpFactory root() { + return instance; + } + + public HttpFactory createChild() { + return new HttpFactory(this); + } + + public Map getDefaultHeader() { + return new HashMap(defaultHeaders); + } + + public HttpFactory addDefaultHeader(String name, String value) { + defaultHeaders.put(name, value); + return this; + } + + public HttpFactory removeDefaultHeader(String name) { + defaultHeaders.remove(name); + return this; + } + + public HttpFactory addDefaultCookie(HttpCookie cookie) { + if (cookie != null) defaultCookies.add(cookie); + return this; + } + + public HttpFactory addDefaultCookie(HttpCookie... cookies) { + if (cookies != null) { + for (HttpCookie c : cookies) { + if (c != null) defaultCookies.add(c); + } + } + return this; + } + + public HttpFactory removeDefaultCookie(String name) { + HttpCookie cookie = null; + for (HttpCookie c : defaultCookies) { + if (c.getName().equals(name)) { + cookie = c; + break; + } + } + defaultCookies.remove(cookie); + return this; + } + + public HttpClient open(URL url) { + return new HttpClient(this, url); + } +}