1.为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。

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
* @ClassName: SSLClient
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:42:07
*
*/
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));
}
}

2.创建一个利用HttpClient发送post请求的工具类

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/**
* 利用HttpClient进行post请求的工具类
* @ClassName: HttpClientUtil
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:43:38
*
*/
public class HttpClientUtil {
@SuppressWarnings("resource")
public static String doPost(String url,String jsonstr,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);
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.测试代码

public static void main(String[] args){
String url = "https://192.168.1.101/xxx";
String jsonStr = "{xxx}";
String httpOrgCreateTestRtn = HttpClientUtil.doPost(url, jsonStr, "utf-8");
}

java利用HttpClient进行https接口调用的更多相关文章

  1. JAVA利用HttpClient进行POST请求(HTTPS)

    目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求. 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的p ...

  2. JAVA的免费天气api接口调用示例

    step1:选择本文所示例的接口"免费天气api" url:https://www.juhe.cn/docs/api/id/39/aid/87 step2:每个接口都需要传入一个参 ...

  3. 利用JS-SDK微信分享接口调用(后端.NET)

    一直都想研究一下JS-SDK微信分享的接口调用,由于最近工作需要,研究了一下,目前只是实现了部分接口的调用:其他接口调用也是类似的: 在开发之前,需要提前准备一个微信公众号,并且域名JSAPI 配置接 ...

  4. [转]java 关于httpclient 请求https (如何绕过证书验证)

    原文:http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两 ...

  5. 基于apache httpclient的常用接口调用方法

    现在的接口开发,大部分是基于http的请求和处理,现在整理了一份常用的调用方式工具类 package com.xh.oms.common.util; import java.io.BufferedRe ...

  6. 非JAVA WEB项目提供Http接口调用实现

    package com.monitor.app.utils; import com.alibaba.fastjson.JSON; import com.google.gson.Gson; import ...

  7. Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用

    近来由于项目需要,需要用到手机短信验证码的功能,其中最主要的是用到了第三方提供的短信平台接口WebService客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限, ...

  8. Https接口调用工具类

    ClientUtil.java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org. ...

  9. Java利用MethodHandle实现反射时调用super的method

    一:实现 1.Base类的实现 package me.silentdoer.reflecsuper; /** * @author silentdoer * @version 1.0 * @descri ...

随机推荐

  1. OD 实验(十) - 对一个 VB 程序的逆向

    前话: VB 程序用 OD 进行逆向的话,可以先查找相关的变量和字符串,以寻找突破口 变量: __vbaVarTstEq __vbaVarCompEq __vbaVarTstNe __vbaVarCo ...

  2. 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Reminders Lab: Part 2 This chapter covers capturing user input through the ...

  3. JQueryDOM节点操作

    你一.JQueryDom节点操作 2.1查找节点 获取p节点 var $pDm=$("p"); 输出p节点的title属性 alert($pDm.attr("title& ...

  4. No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer解决方法

    org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.poj ...

  5. DDD-EF-数据仓储

    关系图 一.分层 二.DomainObjects构建edmx 三.EFRepositoryDemo.Domain定义仓储接口 public interface IRepository<T> ...

  6. <mvc:annotation-driven />讲解

    <mvc:annotation-driven />是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学者快速应用默认配置方案.<mvc:annotation-driv ...

  7. 安装atop笔记

    atop 官网: https://www.atoptool.nl/downloadatop.php 1.直接下载源码安装: https://www.atoptool.nl/download/atop- ...

  8. glTexGen

    [glTexGen] Rather than having to explicitly provide a texture coordinate for each vertex, we can use ...

  9. new 约束

    [new 约束] new 约束指定泛型类声明中的任何类型参数都必须有公共的无参数构造函数.如果要使用 new 约束,则该类型不能为抽象类型. 当泛型类创建类型的新实例,请将 new 约束应用于类型参数 ...

  10. Unity3d 下websocket的使用

    今天介绍一下如何在Unity3D下使用WebSocket. 首先介绍一下什么是websocket,以及与socket,和http的区别与联系,然后介绍一下websocket的一些开源的项目. WebS ...