java https 例子_httpclient https例子

之前很早写过,又用到了,还要baidu,记录一下,回头就自己用了

package com.neusoft.sample.opscopereq;

import java.io.InputStream;

import java.nio.charset.Charset;

import java.security.KeyStore;

import java.security.SecureRandom;

import java.security.cert.CertificateFactory;

import java.util.Base64;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.TrustManagerFactory;

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class M {

public static void main(String[] args) throws Exception {

InputStream inputStream = M.class.getClassLoader().getResourceAsStream("sssssss.crt");

CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

keyStore.load(null);

String certificateAlias = Integer.toString(0);

keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(inputStream));

inputStream.close();

SSLContext sslContext = SSLContext.getInstance("TLS");

TrustManagerFactory trustManagerFactory = TrustManagerFactory

.getInstance(TrustManagerFactory.getDefaultAlgorithm());

trustManagerFactory.init(keyStore);

sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());

CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new   HostnameVerifier() {

public boolean verify(String hostname, SSLSession session) {

// TODO Auto-generated method stub

return true;

}

})   .build();

HttpPost http = new HttpPost("www.url.com");

String USERNAME = "user";

String PASSWORD = "psw";

String auth = USERNAME + ":" + PASSWORD;

byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));

String AUTH_HEADER_VAL = "Basic " + new String(encodedAuth);

http.setHeader("Authorization", AUTH_HEADER_VAL);

JSONObject paramObj = new JSONObject();

paramObj.put("industryCode", "517");

paramObj.put("keyWord", "批发");

paramObj.put("pageNum", 1);

HttpEntity entity=new StringEntity(paramObj.toJSONString(), "application/json;", "utf-8");

http.setEntity(entity);

CloseableHttpResponse res = client.execute(http);

String aa = EntityUtils.toString( res.getEntity() );

System.out.println(aa);

}

}

org.apache.httpcomponents

httpcore

4.4.10

org.apache.httpcomponents

httpclient

4.5.6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值