HttpClient

package com.mediaforce.crawl.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class HttpClientUtil {
    private static Logger logger = Logger.getLogger(HttpClientUtil.class);
    public static String doPost(String url,Map<String,String> map,String charset){
        logger.info("doPost start");
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT,2000);
            httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT,2000);
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
            while(iterator.hasNext()){
                Entry<String,String> elem = (Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            logger.error("dopost error",ex);
        }
        logger.info("doPost over result:"+result);
        return result;
    }
}

SSLClient

package com.mediaforce.crawl.util;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

public class SSLClient extends DefaultHttpClient{
    public SSLClient() throws Exception{
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }
}

基于httpClient的https的无秘钥登陆的更多相关文章

  1. linux配置无秘钥登陆

    linux配置无秘钥登陆 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近有点心烦,很少写博客了,后期的3个月里,估计更新的频率也不会太高,请见谅,今天给大家共享一下linux不 ...

  2. rsh 无秘钥登陆配置

    /etc/hosts.equiv里的主机不须要提供password就能够訪问本机./etc/host.equiv 要和~/.rhosts文件连用. [root@web-htl2-01 ~]# cat ...

  3. Hadoop.2.x_无秘钥设置

    1.在实际生产环境中为Hadoop配置无秘钥登录非常有必要 # 在没有配置时: [liuwl@linux-66-64 hadoop-2.5.0]$ jps 26163 Jps [liuwl@linux ...

  4. HTTPS 之共享秘钥 公钥 及 私钥

    HTTPS 之共享秘钥 公钥 及 私钥一 共享秘钥1.1 概念共享秘钥和我们生活中同一把锁的钥匙概念类似,对同一把锁来说,加锁时使用什么钥匙,解锁也必须使用同样的钥匙. 1.2 共享秘钥在HTTP传输 ...

  5. ssh无秘钥登录

    服务器1:172.16.20.136 服务器2:172.16.20.134 在服务器2上输入:ssh-keygen -t rsa           (一路回车) scp /root/.ssh/id_ ...

  6. ansible-playbook 打通ssh无秘钥

    建议参考: http://www.cnblogs.com/jackchen001/p/6514018.html 这个代码清晰,效果佳! 参考链接: http://www.cnblogs.com/cao ...

  7. 服务器做ssh免秘钥登陆

    集群内服务器做非root用户免秘钥登陆:1.node1新建用户abc1,制作公钥.私钥(一路回车键即可)ssh-keygen –t rsa将自动在/home/abc1/.ssh/目录下创建公私钥文件如 ...

  8. 配置ssh秘钥登陆

    环境2台linux服务器:node1 192.168.X.234                               node2 192.168.X.242 演示用node1无密码登陆node ...

  9. 配置SSH无秘钥登录

    [hadoop@hadoop01 ~]$ cd .ssh [hadoop@hadoop01 .ssh]$ ls authorized_keys id_rsa id_rsa.pub known_host ...

随机推荐

  1. 助手系列之连接mysql数据库

    import MySQLdbdef main(): try: conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='pass',db='a ...

  2. Visual Studio常用插件

    Visual Assist X 番茄不用说了,C# C++编码必备 Image Insertion 可以在代码编辑器中插入图片注释,让代码像Word文档一样图文并茂. 但注意必须是PNG格式的图片,直 ...

  3. (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)

    /* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...

  4. JQuery基础二

    1.表单过滤器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  5. 五、Standard Controllers

    1.standardController:标准的controller 是同页面的standard Object的界面button的功能和逻辑是一样的<apex:page standardCont ...

  6. char类型的字节数

    java为:两个字节,C语言中为:1个字节

  7. [课程设计]Scrum 2.5 多鱼点餐系统开发进度(下单一览页面-菜式添加框架设计)

    Scrum 2.5 多鱼点餐系统开发进度  (下单一览页面-菜式添加框架设计) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题 ...

  8. 【leetcode❤python】 400. Nth Digit

    #-*- coding: UTF-8 -*- class Solution(object):    def findNthDigit(self, n):        ""&quo ...

  9. assigning to 'id<UIGestureRecognizerDelegate> _Nullable' from incompatible

    tip:参考 http://stackoverflow.com/questions/9861538/assigning-to-iddelegate-from-incompatible-type-vie ...

  10. 没有技术说明文档的开源都是耍流氓:微软Roslyn编译即服务在CIIP中具体应用(上)

    前段时间我发布了 github开源:企业级应用快速开发框架CIIP WEB+WIN+移动端,很多园友们都表示支持并与我探讨相关技术问题,上篇中我也承诺会写相关的技术文章,本篇就来介绍一下建模模块中使用 ...