package org.rx.socks.http;
import com.google.common.base.Strings;
import lombok.SneakyThrows;
import org.rx.common.App;
import org.rx.common.Contract;
import org.rx.common.SystemException;
import org.rx.socks.Sockets;
import org.rx.io.IOStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.rx.common.Contract.eq;
import static org.rx.common.Contract.isNull;
/**
* http://www.jianshu.com/p/aa3f066263ed
*/
public class HttpClient {
//region StaticMembers
@SneakyThrows
public static String urlEncode(String str) {
if (Strings.isNullOrEmpty(str)) {
return "";
}
return URLEncoder.encode(str, Contract.Utf8).replace("+", "%20");
}
public static Map<String, String> parseQueryString(String queryString) {
Map<String, String> map = new LinkedHashMap<>();
if (queryString == null) {
return map;
}
String[] pairs = queryString.split("&");
try {
for (String pair : pairs) {
int idx = pair.indexOf("=");
String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), Contract.Utf8) : pair;
String value = idx > 0 && pair.length() > idx + 1
? URLDecoder.decode(pair.substring(idx + 1), Contract.Utf8)
: null;
map.put(key, value);
}
} catch (UnsupportedEncodingException ex) {
throw SystemException.wrap(ex);
}
return map;
}
public static String buildQueryString(String baseUrl, Map<String, String> params) {
if (params == null) {
return baseUrl;
}
if (baseUrl == null) {
baseUrl = "";
}
String c = baseUrl.indexOf("?") == -1 ? "?" : "&";
StringBuilder url = new StringBuilder(baseUrl);
for (String key : params.keySet()) {
String val = params.get(key);
url.append(url.length() == baseUrl.length() ? c : "&").append(urlEncode(key)).append("=")
.append(val == null ? "" : urlEncode(val));
}
return url.toString();
}
//endregion
public static final String GetMethod = "GET", PostMethod = "POST";
private static final String FormMimeType = "application/x-www-form-urlencoded", JsonMimeType = "application/json";
private static final String UserAgent = "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36";
private String contentType;
private int timeout;
private String proxyHost;
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public String getProxyHost() {
return proxyHost;
}
public void setProxyHost(String proxyHost) {
this.proxyHost = proxyHost;
}
public HttpClient() {
timeout = App.TimeoutInfinite;
}
public String httpGet(String url) {
return httpGet(url, null);
}
public String httpGet(String url, Map<String, String> params) {
if (params != null && params.size() > 0) {
url = buildQueryString(url, params);
}
return exec(url, GetMethod, null, contentType, timeout);
}
public String httpPost(String url, Map<String, String> params) {
contentType = FormMimeType;
return exec(url, PostMethod, buildQueryString("", params).substring(1), contentType, timeout);
}
public String httpPost(String url, Object jsonEntity) {
contentType = JsonMimeType;
return exec(url, PostMethod, Contract.toJsonString(jsonEntity), contentType, timeout);
}
private String exec(String url, String method, String content, String contentType, int timeout) {
String charset = Contract.Utf8;
try {
URL uri = new URL(url);
HttpURLConnection client = (HttpURLConnection) (proxyHost != null
? uri.openConnection(new Proxy(Proxy.Type.HTTP, Sockets.parseAddress(proxyHost)))
: uri.openConnection());
client.setDoOutput(true);
client.setDoInput(true);
client.setUseCaches(false);
client.setRequestProperty("User-Agent", UserAgent);
client.setRequestProperty("Accept-Charset", charset);
client.setRequestMethod(method);
if (!App.isNullOrEmpty(contentType)) {
client.setRequestProperty("Content-Type", contentType + ";charset=" + charset);
}
if (timeout > App.TimeoutInfinite) {
client.setConnectTimeout(timeout);
client.setReadTimeout(timeout);
}
client.connect();
if (eq(method, PostMethod) && !App.isNullOrEmpty(content)) {
IOStream.writeString(client.getOutputStream(), content, charset);
}
int resCode = client.getResponseCode();
if (resCode != HttpURLConnection.HTTP_OK) {
}
return IOStream.readString(client.getInputStream(), isNull(client.getContentEncoding(), charset));
} catch (Exception ex) {
throw SystemException.wrap(ex);
}
}
}
666 网购半价返利 http://f-li.cn