This commit is contained in:
@@ -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<HttpCookie> cookies;
|
||||
|
||||
private Map<String, String> headers;
|
||||
|
||||
private Map<String, String> 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<String, String> getHeaders() {
|
||||
return headers == null ? null : new HashMap<String, String>(headers);
|
||||
}
|
||||
|
||||
public HttpClient addHeader(String name, String value) {
|
||||
if (headers == null) this.headers = new HashMap<String, String>();
|
||||
headers.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpClient removeHeader(String name) {
|
||||
if (this.headers == null) return this;
|
||||
headers.remove(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<HttpCookie> getCookies() {
|
||||
return cookies == null ? null : new ArrayList<HttpCookie>(cookies);
|
||||
}
|
||||
|
||||
public HttpClient addCookie(HttpCookie cookie) {
|
||||
if (cookies == null) this.cookies = new ArrayList<HttpCookie>();
|
||||
if (cookie != null) cookies.add(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);
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<HttpCookie> defaultCookies = new CopyOnWriteArrayList<HttpCookie>();
|
||||
|
||||
final Map<String, String> defaultHeaders = new ConcurrentHashMap<String, String>();
|
||||
|
||||
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<String, String> getDefaultHeader() {
|
||||
return new HashMap<String, String>(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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user