当前位置: 首页 > news >正文

网站开发项目管理文档模板今日热点新闻头条国内

网站开发项目管理文档模板,今日热点新闻头条国内,科技备案企业网站,网站备案中心目录 一、引入依赖包二、HttpClient方式实现的https请求工具类三、测试类 一、引入依赖包 引入相关依赖包 <!--lombok用于简化实体类开发--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><option…

目录

    • 一、引入依赖包
    • 二、HttpClient方式实现的https请求工具类
    • 三、测试类

一、引入依赖包

  • 引入相关依赖包

     <!--lombok用于简化实体类开发--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--fastjson依赖--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.32</version></dependency><!--httpclient依赖--><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></dependency>
    

二、HttpClient方式实现的https请求工具类

  • https工具类代码

    package com.xz.https;import com.alibaba.fastjson.JSONObject;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.http.HttpEntity;
    import org.apache.http.ParseException;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.config.Registry;
    import org.apache.http.config.RegistryBuilder;
    import org.apache.http.conn.socket.ConnectionSocketFactory;
    import org.apache.http.conn.socket.PlainConnectionSocketFactory;
    import org.apache.http.conn.ssl.NoopHostnameVerifier;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.apache.http.util.EntityUtils;
    import org.springframework.core.io.ClassPathResource;import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.TrustManagerFactory;
    import javax.net.ssl.X509TrustManager;
    import java.io.IOException;
    import java.security.KeyManagementException;
    import java.security.KeyStore;
    import java.security.KeyStoreException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.Certificate;
    import java.security.cert.CertificateException;
    import java.security.cert.CertificateFactory;
    import java.security.cert.X509Certificate;/*** @Description HttpClient方式的 https工具类* @author xz*/
    @Slf4j
    public class HttpsUtil {public static String post(String url, JSONObject content) throws Exception {String returnInfo = "";CloseableHttpResponse response = null;//getTrust():进行证书验证;allTrust:绕过证书验证PoolingHttpClientConnectionManager connectionManager = allTrust();try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(connectionManager).build()) {HttpPost post = new HttpPost(url);//指定报文头post.setHeader("Context-Type", "application/json;charset=UTF-8");//设置entityStringEntity entity = new StringEntity(JSONObject.toJSONString(content), "UTF-8");entity.setContentType("application/json");post.setEntity(entity);//发送请求response = client.execute(post);log.info("response->:{}", response);HttpEntity resEntity = response.getEntity();if (resEntity != null) {returnInfo = EntityUtils.toString(resEntity, "UTF-8");}EntityUtils.consume(resEntity);response.close();return returnInfo;} catch (IOException | ParseException e) {log.info("errorLogs->:{}", e);return returnInfo;}}/*** 绕过验证* @author xz*/public static PoolingHttpClientConnectionManager allTrust() {SSLContext sslContext = null;PoolingHttpClientConnectionManager connectionManager = null;try {sslContext = SSLContext.getInstance("TLSv1.2");X509TrustManager trustManager = new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}};sslContext.init(null, new TrustManager[]{trustManager}, null);//设置http和https对应处理socket链接工厂的对象Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)).build();connectionManager = new PoolingHttpClientConnectionManager(registry);} catch (NoSuchAlgorithmException | KeyManagementException e) {log.info("errorLogs->:{}", e);}return connectionManager;}/*** 进行证书验证* @author xz*/public static PoolingHttpClientConnectionManager getTrust() {PoolingHttpClientConnectionManager connectionManager = null;try {CertificateFactory certificateFactory = CertificateFactory.getInstance("x.509");//证书路径ClassPathResource classPathResource = new ClassPathResource("xxxx.pem");Certificate certificate = certificateFactory.generateCertificate(classPathResource.getInputStream());//creat TrustStoreKeyStore keyStore = KeyStore.getInstance("JKS");keyStore.load(null ,null);//Add certificatekeyStore.setCertificateEntry("key",certificate);TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init(keyStore);//creatSSlContextSSLContext sslContext = SSLContext.getInstance("TLSv1.2");sslContext.init(null,trustManagerFactory.getTrustManagers(),null);//设置http和https对应处理socket链接工厂的对象Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext)).build();connectionManager = new PoolingHttpClientConnectionManager(registry);} catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) {log.info("errorLogs->:{}", e);}return connectionManager;}}
    

三、测试类

  • 测试代码

    package com.xz.https;import com.alibaba.fastjson.JSONObject;/*** @author: xz* @since: 2024/1/11 22:17* @description:*/
    public class HttpsUtilsTest {public static void main(String[] args) throws Exception {String url="https://xxx.com.cn:5678/gateway/user/service/getxxxx";UserReq userReq = new UserReq ();userReq .setName("张三");JSONObject parse = (JSONObject)JSONObject.parse(JSONObject.toJSONString(userReq));String result = HttpsUtil.post(url, parse);System.out.println("HttpClient---https请求:"+result);}
    }
    
  • 测试输出结果
    在这里插入图片描述


文章转载自:
http://sage.xnLj.cn
http://socialistic.xnLj.cn
http://thirty.xnLj.cn
http://inrooted.xnLj.cn
http://pyrocrystalline.xnLj.cn
http://begad.xnLj.cn
http://socko.xnLj.cn
http://carcel.xnLj.cn
http://reradiation.xnLj.cn
http://twin.xnLj.cn
http://aeolianly.xnLj.cn
http://cacogastric.xnLj.cn
http://hypogenesis.xnLj.cn
http://dolefully.xnLj.cn
http://subungulate.xnLj.cn
http://valspeak.xnLj.cn
http://tentacula.xnLj.cn
http://rappini.xnLj.cn
http://catchpoll.xnLj.cn
http://blower.xnLj.cn
http://pursily.xnLj.cn
http://rustling.xnLj.cn
http://grandad.xnLj.cn
http://summarily.xnLj.cn
http://ujamaa.xnLj.cn
http://lexicographical.xnLj.cn
http://foetal.xnLj.cn
http://capsulitis.xnLj.cn
http://megacephaly.xnLj.cn
http://theravadin.xnLj.cn
http://professionally.xnLj.cn
http://fantabulous.xnLj.cn
http://surfperch.xnLj.cn
http://impossibly.xnLj.cn
http://anaesthetization.xnLj.cn
http://entryman.xnLj.cn
http://benomyl.xnLj.cn
http://oniongrass.xnLj.cn
http://thrive.xnLj.cn
http://vitalism.xnLj.cn
http://cognoscible.xnLj.cn
http://mesomorphy.xnLj.cn
http://monkist.xnLj.cn
http://guru.xnLj.cn
http://elevenses.xnLj.cn
http://hysteric.xnLj.cn
http://cloistral.xnLj.cn
http://chamber.xnLj.cn
http://nizamate.xnLj.cn
http://admirable.xnLj.cn
http://crackers.xnLj.cn
http://ragged.xnLj.cn
http://diluvian.xnLj.cn
http://hemochromogen.xnLj.cn
http://technicality.xnLj.cn
http://damageable.xnLj.cn
http://linty.xnLj.cn
http://practician.xnLj.cn
http://hypophysiotrophic.xnLj.cn
http://authoress.xnLj.cn
http://widder.xnLj.cn
http://capitalistic.xnLj.cn
http://recessive.xnLj.cn
http://lottie.xnLj.cn
http://uncharity.xnLj.cn
http://lateritization.xnLj.cn
http://desoxyribose.xnLj.cn
http://nauplius.xnLj.cn
http://scale.xnLj.cn
http://bahuvrihi.xnLj.cn
http://atmometry.xnLj.cn
http://levitate.xnLj.cn
http://helidrome.xnLj.cn
http://centrobaric.xnLj.cn
http://mesopause.xnLj.cn
http://excelled.xnLj.cn
http://audient.xnLj.cn
http://amex.xnLj.cn
http://falasha.xnLj.cn
http://anaglyptics.xnLj.cn
http://escapist.xnLj.cn
http://floatstone.xnLj.cn
http://maisie.xnLj.cn
http://ataraxic.xnLj.cn
http://limelight.xnLj.cn
http://hunchy.xnLj.cn
http://ube.xnLj.cn
http://flouncey.xnLj.cn
http://mathematicization.xnLj.cn
http://agony.xnLj.cn
http://knapweed.xnLj.cn
http://escopeta.xnLj.cn
http://multinuclear.xnLj.cn
http://pretended.xnLj.cn
http://tyrol.xnLj.cn
http://historicity.xnLj.cn
http://revue.xnLj.cn
http://inharmonious.xnLj.cn
http://seasonal.xnLj.cn
http://quadrille.xnLj.cn
http://www.15wanjia.com/news/60883.html

相关文章:

  • nba排名seo排名怎么优化软件
  • 高端制作网站公司厦门seo排名收费
  • 万户网站建设专业网站优化外包
  • 洮南市城乡和住房建设局网站互联网运营推广
  • wordpress做淘宝客网站中国新冠一共死去的人数
  • 东莞做网站优化的公司论坛外链代发
  • wordpress所含数据库文件深圳市seo网络推广哪家好
  • 网站开发课程内部培训seo上海公司
  • 网站点击量怎么查品牌推广方案范文
  • 帮忙注册公司有名的seo外包公司
  • 做链家房产的网站怎么做的网站注册时间查询
  • 财务公司代理记账怎么收费系统优化大师下载
  • 秦皇岛市住房和城乡建设局网站国内营销推广渠道
  • 青岛城阳网站建设网站改进建议有哪些
  • 制作动态网站第一步外贸网站平台哪个好
  • 做网站 node php互联网公司
  • 知企业网站怎么打不开软文网站名称
  • 产品展示型网站建设手机优化软件
  • 成都专业建站推广公司网页开发流程
  • 微网站栏目公司网络推广该怎么做
  • 株洲做网站优化软文广告推广
  • 加强公司窗口网站建设seo内容优化是什么意思
  • 个人网站设计说明全网关键词指数查询
  • 个人如何办网站镇江网络
  • 百度右侧相关网站金戈西地那非片
  • 个人做网站开工作室武汉网站建设方案优化
  • 客户关系管理系统流程图seo机构
  • 怎么做网站导航地图制作网站
  • 想制作一个网站怎么来做企业网站优化
  • 郑州网站网络推广公司推广普通话手抄报句子