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();
}
}

java如何发起https请求的更多相关文章

  1. 【问题记录】Java服务发起HTTPS请求报错:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException

    问题报错 今天上线了我开发的一个OAuth2单点登录客户端的实现,在测试系统验证没问题,到生产环境由于单点登录服务端HTTPS协议,报错如下: I/O error on POST request fo ...

  2. java实现 HTTP/HTTPS请求绕过证书检测代码实现

    java实现 HTTP/HTTPS请求绕过证书检测代码实现 1.开发需求 需要实现在服务端发起HTTP/HTTPS请求,访问其他程序资源. 2.URLConnection和HTTPClient的比较 ...

  3. Java实现 HTTP/HTTPS请求绕过证书检测

    java实现 HTTP/HTTPS请求绕过证书检测 一.Java实现免证书访问Https请求 创建证书管理器类 import java.security.cert.CertificateExcepti ...

  4. php 使用curl发起https请求

    今天一个同事反映,使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Details: erro ...

  5. 老李分享:curl发起https请求

    老李分享:curl发起https请求 在POPTEST上课的过程中,我们需要本地模拟https请求来完成性能测试,我们用curl来实现,curl是利用URL语法在命令行方式下工作的开源文件传输工具,使 ...

  6. http 使用curl发起https请求报错的解决办法

    使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Details: error:1409008 ...

  7. 用curl发起https请求

    使用curl发起https请求 使用curl如果想发起的https请求正常的话有2种做法: 方法一.设定为不验证证书和host. 在执行curl_exec()之前.设置option $ch = cur ...

  8. java实现的https请求

    转载并修改自 http://www.blogjava.net/etlan/archive/2006/06/29/55767.html Https请求 超文本传输协议HTTP协议:被用于在Web浏览器和 ...

  9. 使用curl发起https请求

    "SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:S ...

随机推荐

  1. java-事务-案例

    项目结构: 数据库: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.53 : Database - threadlocal ************** ...

  2. 数据导入报错 Got a packet bigger than‘max_allowed_packet’bytes

    数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max_a ...

  3. mysql编译安装后各种常见错误集锦

    1.ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', sys ...

  4. 关于Unity中使用刚体制作简单跑酷案例

    一.步骤 1.创建一个Canvas 2.对Canvas进行初始化,记得把Game视图的分辨率调成和Canvas里面设置的一样的分辨率960X640 3.创建一个Image的UI节点作为Canvas的子 ...

  5. 关于Cocos2d-x中文乱码问题的解决

    方法一: 1.首先,复制下面的代码,创建一个icov,h的头文件,并放在项目目录下 #include "stdlib.h"#include "string.h" ...

  6. 【转】【OPenGL】opengl 64位 配置 freeglutx64下载

    1.GLEW The OpenGL Extension Wrangler Library (GLEW) is a cross-platform open-source C/C++ extension ...

  7. 不可在 for 循环体内修改循环变量,防止 for 循环失去控制

    不可在 for 循环体内修改循环变量,防止 for 循环失去控制. #include <iostream> /* run this program using the console pa ...

  8. 使用avahi 的mdns服务发现server

    avahi-browse -a 可以查看局域网内所有的mdns服务, avahi-browse -r _xxxxx._tcp

  9. 一个窗口里包含一个iframe,点击iframe内的submit按钮,返回的视图总是显示在iframe中,我想要的效果是点击按钮后返回的视图是在浏览器窗口中...?asp.net mvc 的action中,不用js怎么实现??????????

    Content("<script type='text/javascript'>parent.location.href = '" + url + "';&l ...

  10. 使用Base SDK 6.1编译的APP在iOS7的设备上运行,NavigationBar覆盖view的解决办法

    if (__IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_6_1) { self.navigationController.navigationBar.tr ...