HttpClent,SSL

简介: import java.security.NoSuchAlgorithmException;import javax.net.ssl.SSLContext;import org.
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();
    }
}
目录
相关文章
|
3天前
|
安全 算法 网络安全
SSL/TLS协议是什么?
SSL/TLS协议是什么?
75 57
|
6月前
|
存储 安全 网络安全
什么是SSL/TLS协议?
【5月更文挑战第14天】什么是SSL/TLS协议?
204 0
|
6月前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
139 0
|
6月前
|
安全 算法 应用服务中间件
TLS SSL
TLS SSL
92 0
|
域名解析 网络协议 应用服务中间件
部署ssl
部署ssl
97 1
|
网络安全
SSL 配置
SSL 配置
138 1
|
安全 数据建模 网络安全
关于 SSL 证书
在 HTTPS 协议大行其道的今天,其通信所需要的 SSL 证书也是不可或缺的一环,如果访问没有 SSL 证书的网站,就是下面这样的
276 0
关于 SSL 证书
|
Web App开发 网络协议 安全
聊聊HTTPS和SSL/TLS协议
要说清楚 HTTPS 协议的实现原理,至少需要如下几个背景知识。 1. 大致了解几个基本术语(HTTPS、SSL、TLS)的含义 2. 大致了解 HTTP 和 TCP 的关系(尤其是“短连接”VS“长连接”) 3. 大致了解加密算法的概念(尤其是“对称加密与非对称加密”的区别) 考虑到很多技术菜鸟可能不了解上述背景,俺先用最简短的文字描述一下。
1846 0
|
应用服务中间件 网络安全 nginx
|
Web App开发 安全 测试技术