HttpClent,SSL

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

HttpClent,SSL的相关文章

iis内网程序想用https访问,ssl证书怎么弄

问题描述 iis内网程序想用https访问,ssl证书怎么弄 程序部署在内网iis服务器上,与外网物理隔离,内网只能通过ip地址访问,没有dns服务器,怎么用h?ttps访问? 解决方案 https一样支持ip方式,自己制作一个对应IP地址的证书就可以了. 解决方案二: 免费StartSSL证书申请详细步骤和IIS中的SSL设置 http://www.server110.com/iis/201407/10798.html 解决方案三: 用hosts文件代替dns 解决方案四: 沃通(WoSign

Radware推出全新Alteon ADC产品,SSL性能在业内遥遥领先

本文讲的是 Radware推出全新Alteon ADC产品,SSL性能在业内遥遥领先,日前,全球领先的网络安全和应用交付解决方案提供商Radware公司推出了全新的Alteon D系列设备与软件,意在解决日益增多的加密流量.加密攻击和恶意软件以及不断增长的数据中心自动化需求带来的诸多市场挑战.Radware Alteon D系列提供了全系列的扩展软件模块,以及可扩展应用交付控制器(ADC)功能的订阅服务. Radware Alteon D系列硬件及虚拟设备中将SSL性能提高至新的水平,SSL连接

Tomcat5.0+MySql配置JDBC,DBCP,SSL

mysql 准备环境: 1.j2sdk-1_4_2-windows-i586.exe //jdk 2.mysql-4.0.20d-win.zip //mysql数据库 3.mysqlcc-0.9.4-win32.zip //mysqlGUI控制 4.jakarta-tomcat-5.0.27.exe //tomcat服务器 5.mysql-connector-java-3.0.14-production.zip //内含mysql驱动 安装步骤: 1.安装jdk 2.安装tomcat 3.安装m

金融行业亲睐GlobalSign,SSL数字证书保障安全

中介交易 SEO诊断 淘宝客 云主机 技术大厅 上海(2013 年 10 月 14日)-- 网银用户越来越多,网络欺诈行为时刻考验着银行的安全策略.保护用户的敏感信息和资产安全是银行的重大课题.虽然动态口令等策略加强了用户的使用安全,但 也增加的认证环节.还需记忆多个密码等因素也大大减弱了网上银行的便捷性,另外,钓鱼网站的仿真度越来越高,其程度足以欺骗业内人士,甚至还有网站使用了 不可信证书.此类情况都是用户使用安全的威胁. 越来越多的网站特别是金融领域开始部署 GlobalSign 数字证书用

其实,SSL也不是配通了就什么都不管的~~

其中太多的中间人攻击需要去加强加固~~ 测试过A级是必须的!! https://www.ssllabs.com/ssltest/ 这网址两年前,我写过的哈  

PHP判断服务器SSL是否启用,也就是判断是否支持HTTPS连接

通过php函数来检测服务器是否支持SSL(即HTTPS连接),具体函数代码如下.另外关于如何开启SSL支持可参照本站文章: php检测服务器SSL是否开启以及开启SSL的方法 /* * 检测服务器是否支持SSL连接(Https连接) * @return bool */ function is_SSL(){ if(!isset($_SERVER['HTTPS'])) return FALSE; if($_SERVER['HTTPS']===1){ //Apache return TRUE; }el

asp.net如何用ssl加密进行证书验证login.aspx登陆 ,急急。。。

问题描述 网站做好,IIS也已经配置好了,SSL都配置好了,可以通过https访问网站,但是我在登录的时候有问题,我的机器装了3个证书,不管选哪个,都能登录,这不符合我的要求,我把其中一个证书的缩微图与用户绑定,只有这个证书才能登录,下边的这段代码是别人在网站已经能使用的,通过WCF通信,但是在我这里报"未将对象引用设置到对象实例,求大家帮帮我,礼拜一要用,实在没办法了......publicstringSys_Public_CertificateInfo(){stringfunctionRet

服务器管理助手Linux版(宝塔)新版安装,支持一键SSL配置

安装要求:   内存:128M以上,推荐512M以上(纯面板约占系统10M内存) 硬盘:100M以上可用硬盘空间(纯面板约占20M磁盘空间) 系统:暂时只对 CentOS 6.x / 7.x 提供支持(暂不支持 Ubuntu.Debian): 确保是干净的操作系统,没有安装过其它环境带的Apache/Nginx/php/MySQL   注意事项: 如果你有数据盘,请先挂载到www目录在进行安装!!     Linux面板3.7安装脚本:  复制代码 yum install -y wget &&am

解密SSL流量,发现隐藏威胁

加密传输技术进一步得到应用,一方面保护了个人隐私信息,另外一方面也助长了犯罪行为. 随着加密传输技术进一步得到应用,过去的五年中,谷歌.YouTube.推特等大公司使用 SSL 的行为推动了其它互联网企业使用加密连接的热情. 加密传输也带来了风险.由于加密信息对第三方不公开,网络罪犯也可以在不引人注意的情况下从事犯罪活动.他们使用传输层安全协议(Transport Layer Security,TLS)和安全套接层协议(Secure Socket Layer,SSL)加密其通信. 来自 Palo