This commit is contained in:
@@ -9,7 +9,7 @@ import java.net.*;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* 待开发……
|
* 待开发……
|
||||||
*
|
*
|
||||||
* @see http://www.redkale.org
|
* @see http://www.redkale.org
|
||||||
@@ -17,18 +17,20 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public final class HttpClient {
|
public final class HttpClient {
|
||||||
|
|
||||||
protected List<HttpCookie> cookies;
|
protected Map<String, HttpCookie> cookies;
|
||||||
|
|
||||||
protected Map<String, String> headers;
|
protected Map<String, String> headers;
|
||||||
|
|
||||||
protected Map<String, String> params;
|
protected Map<String, String> params;
|
||||||
|
|
||||||
protected final HttpFactory factory;
|
protected final HttpFactory root;
|
||||||
|
|
||||||
protected final URL url;
|
protected final URL url;
|
||||||
|
|
||||||
HttpClient(HttpFactory factory, URL url) {
|
private String method = "GET";
|
||||||
this.factory = factory;
|
|
||||||
|
HttpClient(HttpFactory root, URL url) {
|
||||||
|
this.root = root;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,52 +38,60 @@ public final class HttpClient {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HttpClient getMethod() {
|
||||||
|
this.method = "GET";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpClient postMethod() {
|
||||||
|
this.method = "POST";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, String> getHeaders() {
|
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) {
|
public HttpClient addHeader(String name, String value) {
|
||||||
if (headers == null) this.headers = new HashMap<String, String>();
|
if (this.headers == null && name != null) this.headers = new HashMap<String, String>();
|
||||||
headers.put(name, value);
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpClient removeHeader(String name) {
|
public HttpClient removeHeader(String name) {
|
||||||
if (this.headers == null) return this;
|
if (this.headers == null || name == null) return this;
|
||||||
headers.remove(name);
|
headers.remove(name);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<HttpCookie> getCookies() {
|
public Map<String, HttpCookie> getCookies() {
|
||||||
return cookies == null ? null : new ArrayList<HttpCookie>(cookies);
|
return this.cookies == null ? null : new HashMap<String, HttpCookie>(this.cookies);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpClient addCookie(HttpCookie cookie) {
|
public HttpClient addCookie(final HttpCookie cookie) {
|
||||||
if (cookies == null) this.cookies = new ArrayList<HttpCookie>();
|
if (this.cookies == null) this.cookies = new HashMap<String, HttpCookie>();
|
||||||
if (cookie != null) cookies.add(cookie);
|
if (cookie != null) this.cookies.put(cookie.getName(), cookie);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpClient addCookie(HttpCookie... cookies) {
|
public HttpClient addCookie(final HttpCookie... httpCookies) {
|
||||||
if (cookies == null) this.cookies = new ArrayList<HttpCookie>();
|
if (httpCookies == null || httpCookies.length < 1) return this;
|
||||||
if (cookies != null) {
|
if (this.cookies == null) this.cookies = new HashMap<String, HttpCookie>();
|
||||||
for (HttpCookie c : cookies) {
|
for (HttpCookie c : httpCookies) {
|
||||||
if (c != null) this.cookies.add(c);
|
if (c != null) this.cookies.put(c.getName(), c);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpClient removeCookie(String name) {
|
public HttpClient removeCookie(final String name) {
|
||||||
if (this.cookies == null) return this;
|
if (this.cookies != null && name != null) this.cookies.remove(name);
|
||||||
HttpCookie cookie = null;
|
|
||||||
for (HttpCookie c : cookies) {
|
|
||||||
if (c.getName().equals(name)) {
|
|
||||||
cookie = c;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cookies.remove(cookie);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public class HttpFactory {
|
|||||||
|
|
||||||
final HttpFactory parent;
|
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>();
|
final Map<String, String> defaultHeaders = new ConcurrentHashMap<String, String>();
|
||||||
|
|
||||||
@@ -56,28 +56,21 @@ public class HttpFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HttpFactory addDefaultCookie(HttpCookie cookie) {
|
public HttpFactory addDefaultCookie(HttpCookie cookie) {
|
||||||
if (cookie != null) defaultCookies.add(cookie);
|
if (cookie != null) defaultCookies.put(cookie.getName(), cookie);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpFactory addDefaultCookie(HttpCookie... cookies) {
|
public HttpFactory addDefaultCookie(HttpCookie... cookies) {
|
||||||
if (cookies != null) {
|
if (cookies != null) {
|
||||||
for (HttpCookie c : cookies) {
|
for (HttpCookie c : cookies) {
|
||||||
if (c != null) defaultCookies.add(c);
|
if (c != null) defaultCookies.put(c.getName(), c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpFactory removeDefaultCookie(String name) {
|
public HttpFactory removeDefaultCookie(String name) {
|
||||||
HttpCookie cookie = null;
|
defaultCookies.remove(name);
|
||||||
for (HttpCookie c : defaultCookies) {
|
|
||||||
if (c.getName().equals(name)) {
|
|
||||||
cookie = c;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defaultCookies.remove(cookie);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user