This commit is contained in:
wentch
2015-12-25 11:51:19 +08:00
parent 610c4f5925
commit 54fa62cb98
2 changed files with 44 additions and 41 deletions

View File

@@ -9,7 +9,7 @@ import java.net.*;
import java.util.*;
/**
*
*
* 待开发……
*
* @see http://www.redkale.org
@@ -17,18 +17,20 @@ import java.util.*;
*/
public final class HttpClient {
protected List<HttpCookie> cookies;
protected Map<String, HttpCookie> cookies;
protected Map<String, String> headers;
protected Map<String, String> params;
protected final HttpFactory factory;
protected final HttpFactory root;
protected final URL url;
HttpClient(HttpFactory factory, URL url) {
this.factory = factory;
private String method = "GET";
HttpClient(HttpFactory root, URL url) {
this.root = root;
this.url = url;
}
@@ -36,52 +38,60 @@ public final class HttpClient {
return this;
}
public HttpClient getMethod() {
this.method = "GET";
return this;
}
public HttpClient postMethod() {
this.method = "POST";
return this;
}
public Map<String, String> getHeaders() {
return headers == null ? null : new HashMap<String, String>(headers);
return this.headers == null ? null : new HashMap<String, String>(this.headers);
}
public HttpClient addHeader(String name, String value) {
if (headers == null) this.headers = new HashMap<String, String>();
headers.put(name, value);
if (this.headers == null && name != null) this.headers = new HashMap<String, String>();
this.headers.put(name, value);
return this;
}
public HttpClient addHeader(final Map<String, String> headerMap) {
if (headerMap == null || headerMap.isEmpty()) return this;
if (this.headers == null) this.headers = new HashMap<String, String>();
this.headers.putAll(headerMap);
return this;
}
public HttpClient removeHeader(String name) {
if (this.headers == null) return this;
if (this.headers == null || name == null) return this;
headers.remove(name);
return this;
}
public List<HttpCookie> getCookies() {
return cookies == null ? null : new ArrayList<HttpCookie>(cookies);
public Map<String, HttpCookie> getCookies() {
return this.cookies == null ? null : new HashMap<String, HttpCookie>(this.cookies);
}
public HttpClient addCookie(HttpCookie cookie) {
if (cookies == null) this.cookies = new ArrayList<HttpCookie>();
if (cookie != null) cookies.add(cookie);
public HttpClient addCookie(final HttpCookie cookie) {
if (this.cookies == null) this.cookies = new HashMap<String, HttpCookie>();
if (cookie != null) this.cookies.put(cookie.getName(), cookie);
return this;
}
public HttpClient addCookie(HttpCookie... cookies) {
if (cookies == null) this.cookies = new ArrayList<HttpCookie>();
if (cookies != null) {
for (HttpCookie c : cookies) {
if (c != null) this.cookies.add(c);
}
public HttpClient addCookie(final HttpCookie... httpCookies) {
if (httpCookies == null || httpCookies.length < 1) return this;
if (this.cookies == null) this.cookies = new HashMap<String, HttpCookie>();
for (HttpCookie c : httpCookies) {
if (c != null) this.cookies.put(c.getName(), 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);
public HttpClient removeCookie(final String name) {
if (this.cookies != null && name != null) this.cookies.remove(name);
return this;
}

View File

@@ -21,7 +21,7 @@ public class HttpFactory {
final HttpFactory parent;
final List<HttpCookie> defaultCookies = new CopyOnWriteArrayList<HttpCookie>();
final Map<String, HttpCookie> defaultCookies = new ConcurrentHashMap<String, HttpCookie>();
final Map<String, String> defaultHeaders = new ConcurrentHashMap<String, String>();
@@ -56,28 +56,21 @@ public class HttpFactory {
}
public HttpFactory addDefaultCookie(HttpCookie cookie) {
if (cookie != null) defaultCookies.add(cookie);
if (cookie != null) defaultCookies.put(cookie.getName(), cookie);
return this;
}
public HttpFactory addDefaultCookie(HttpCookie... cookies) {
if (cookies != null) {
for (HttpCookie c : cookies) {
if (c != null) defaultCookies.add(c);
if (c != null) defaultCookies.put(c.getName(), 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);
defaultCookies.remove(name);
return this;
}