如何使用JAVA请求HTTPS

JDK对应的TLS版本(仅供参考)

1.写一个SSLClient类,继承至HttpClient

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;
//用于进行Https请求的HttpClient
public class SSLClient extends DefaultHttpClient{
    public SSLClient() throws Exception{
        super();     //传输协议需要根据自己的判断 
        SSLContext ctx = SSLContext.getInstance("TLSv1.2");
        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));
    }
}  

2.写一个利用HttpClient发送post请求的类

import java.util.ArrayList;
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.util.EntityUtils;
/*
 * 利用HttpClient进行post请求的工具类
 */
public class HttpClientUtil {
    public String doPost(String url,Map<String,String> map,String charset){
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator 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){
            ex.printStackTrace();
        }
        return result;
    }
}  

3.调用请求的测试代码

import java.util.HashMap;
import java.util.Map;
//对接口进行测试
public class TestMain {
    private String url = "https://192.168.1.101/";
    private String charset = "utf-8";
    private HttpClientUtil httpClientUtil = null;  

    public TestMain(){
        httpClientUtil = new HttpClientUtil();
    }  

    public void test(){
        String httpOrgCreateTest = url + "httpOrg/create";
        Map<String,String> createMap = new HashMap<String,String>();
        createMap.put("authuser","*****");
        createMap.put("authpass","*****");
        createMap.put("orgkey","****");
        createMap.put("orgname","****");
        String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);
        System.out.println("result:"+httpOrgCreateTestRtn);
    }  

    public static void main(String[] args){
        TestMain main = new TestMain();
        main.test();
    }
}  

    用到的架包:  下载

时间: 2024-11-05 17:34:15

如何使用JAVA请求HTTPS的相关文章

Tomcat创建HTTPS访问,java访问https

一 https和ssL HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL. 它是一个URI scheme(抽象标识符体系),句法类同http:体系.用于安全的HTTP数据传输.https:URL表明它使用了HTTP,但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层

cxf webservice请求https

本地java请求https接口,不需要添加证书: 只需要修改配置文件applicationContext-soap-client.xml: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://cxf.apache.org/transports

java 实现https请求

java 实现https请求 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从而不能访问HTTPS站点.本文在简要介绍JSSE的基础上提出了两种解决该问题的方法. 引言 过去的十几年,网络上已经积累了大量的Web应用.如今,无论是整合原有的Web应用系统,还是进行新的Web开发,都要求通过编程来访问某些Web 页面.传统的方法是使用Socket接口,但现在很多开发平台或工具如

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection)

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection) [转] https协议对于开发者而言其实只是多了一步证书验证的过程.这个证书正常情况下被jdk/jre/security/cacerts所管理.里面证书包含两种情况: 1.机构所颁发的被认证的证书,这种证书的网站在浏览器访问时https头显示为绿色如百度 2.个人所设定的证书,这种证书的网站在浏览器里https头显示为红色×,且需要点击信任该网站才能继续访问.而点击信任这一步的操作就是我们

Android 实现 HttpClient 请求Https

如题,默认下,HttpClient是不能请求Https的,需要自己获取 [java] view plaincopy private static final int SET_CONNECTION_TIMEOUT = 5 * 1000; private static final int SET_SOCKET_TIMEOUT = 20 * 1000; public static HttpClient getNewHttpClient() { try { KeyStore trustStore = K

【fastweixin框架教程3】JAVA进行HTTPS网站访问,PKIX path building failed解决方法

上几篇文章我们谈到fastweixin使用问题和修改. 今天的问题就是使用JAVA访问HTTPS网站时候经常出现javax.net.ssl.SSLHandshakeException javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderEx

jmeter的http post请求与测试Java请求

1.jmeter 测试Java请求 1.1 建立测试类,在被测程序中添加测试类 1.2 将测试程序打包,打成不可运行的包 1.3 将打好的包,放在$JMETER_HOME/lib/exts下面,把测试包依赖的jar放在$JMETER_HOME/lib 2.jmeter测试https post http://my.oschina.net/u/147181/blog/164873 2.1 填写好地址及路径,协议修改为https 端口和代理默认即可

[译]HttpClient请求HTTPS URL

1.概览 本文将演示如何配置Apache HttpClient 4 添加ssl支持.目的很简单----无需有效证书即可成功请求 HTTPS URLs. 如果你想深入挖掘和学习其他和HttpClient相关的酷知识,请点击httpclient-guide 延伸阅读: httpclient-connection-management httpclient-advanced-config httpclient-4-cookies 2. SSLPeerUnverifiedException异常 使用ht

jmeter中实现java请求实战日志

jmeter中实现java请求思路:采用jmeter提供的框架包,创建一个类,继承类JavaSamplerClient 重写该类的以下四个方法 getDefaultParameters() runTest(JavaSamplerContext context) setupTest(JavaSamplerContext context) teardownTest(JavaSamplerContext context) 重点是通过getDefaultParameters方法构造jmeter参数,将从