import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
public class HttpConnectionManager {
private static PoolingHttpClientConnectionManager cm = null;
public HttpConnectionManager() {
}
public static CloseableHttpClient getHttpClient() {
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
return httpClient;
}
static {
SSLConnectionSocketFactory sslsf = null;
try {
sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
} catch (NoSuchAlgorithmException var2) {
var2.printStackTrace();
}
Registry socketFactoryRegistry = RegistryBuilder.create().register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setMaxTotal(400);
cm.setDefaultMaxPerRoute(50);
}
}
import com.netstone.utility.HttpConnectionManager;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpRequest {
public HttpRequest() {
}
public static String sendGet(String url, Map<String, Object> params) throws IOException {
return sendGet(url, params, '\uea60');
}
public static String sendGet(String url, Map<String, Object> params, int timeout) throws IOException {
Iterator result;
Entry httpClient;
if(params != null) {
for(result = params.entrySet().iterator(); result.hasNext(); url = url + (String)httpClient.getKey() + "=" + URLEncoder.encode(String.valueOf(httpClient.getValue()), "utf-8")) {
httpClient = (Entry)result.next();
if(url.indexOf("?") == -1) {
url = url + "?";
} else {
url = url + "&";
}
}
}
result = null;
httpClient = null;
HttpGet httpGet = null;
CloseableHttpResponse response = null;
String result1;
try {
CloseableHttpClient httpClient1 = HttpConnectionManager.getHttpClient();
httpGet = new HttpGet(url);
Builder e = RequestConfig.custom();
e.setSocketTimeout(timeout);
e.setConnectTimeout(timeout);
e.setConnectionRequestTimeout(timeout);
RequestConfig requestConfig = e.build();
httpGet.setConfig(requestConfig);
response = httpClient1.execute(httpGet);
if(response.getStatusLine().getStatusCode() != 200) {
throw new IOException("get fail(" + response.getStatusLine().getStatusCode() + "): " + url);
}
HttpEntity entity = response.getEntity();
result1 = EntityUtils.toString(entity);
} catch (IOException var17) {
throw var17;
} finally {
if(response != null) {
try {
response.close();
} catch (Exception var16) {
;
}
}
}
return result1;
}
public static String sendPost(String url, Map<String, Object> params) throws IOException {
return sendPost(url, params, '\uea60');
}
public static String sendPost(String url, Map<String, Object> params, int timeout) throws IOException {
String charset = "UTF-8";
String result = null;
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpConnectionManager.getHttpClient();
httpPost = new HttpPost(url);
ArrayList e = new ArrayList();
if(params != null) {
Iterator urlEncodedFormEntity = params.entrySet().iterator();
while(urlEncodedFormEntity.hasNext()) {
Entry builder = (Entry)urlEncodedFormEntity.next();
BasicNameValuePair requestConfig = new BasicNameValuePair((String)builder.getKey(), new String(String.valueOf(builder.getValue() == null?"":builder.getValue()).getBytes(), charset));
e.add(requestConfig);
}
}
UrlEncodedFormEntity urlEncodedFormEntity1 = new UrlEncodedFormEntity(e, charset);
httpPost.setEntity(urlEncodedFormEntity1);
Builder builder1 = RequestConfig.custom();
builder1.setSocketTimeout(timeout);
builder1.setConnectTimeout(timeout);
builder1.setConnectionRequestTimeout(timeout);
RequestConfig requestConfig1 = builder1.build();
httpPost.setConfig(requestConfig1);
response = httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode() != 200) {
throw new IOException("post fail(" + response.getStatusLine().getStatusCode() + "): " + url);
}
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (IOException var20) {
throw var20;
} finally {
if(response != null) {
try {
response.close();
} catch (Exception var19) {
var19.printStackTrace();
}
}
}
return result;
}
}
import javax.servlet.http.HttpServletRequest;
public class IPUtility {
public IPUtility() {
}
public static String getIP(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
import java.util.Calendar;
import java.util.Date;
public class ExpireUtility {
public ExpireUtility() {
}
public static Date getExpire(int second) {
return getExpire(0, second);
}
public static Date getExpire(int minute, int second) {
return getExpire(0, minute, second);
}
public static Date getExpire(int hour, int minute, int second) {
Calendar calendar = Calendar.getInstance();
calendar.add(13, second);
calendar.add(12, minute);
calendar.add(10, hour);
return calendar.getTime();
}
}
时间: 2024-09-20 00:24:56