首页 > 其他分享 >微信支付请求(带证书)

微信支付请求(带证书)

时间:2023-08-07 19:22:28浏览次数:33  
标签:http 请求 证书 微信 httpost org apache import String

import cn.hutool.core.lang.Pair;
import cn.hutool.crypto.SecureUtil;
import com.dtyunxi.exchange.biz.model.ApiChannelType;
import com.dtyunxi.exchange.biz.utils.OkHttpUtil;
import okhttp3.OkHttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

    private static final String SslContentKey = "certBase64Content";
    private Map<String, CloseableHttpClient> OkHttpClientMap = new HashMap<>();

    
    public Pair<Integer, String> execute(Map<String, Object> requestContext, HttpMethod httpMethod, String url, Map<String, String> headers, Object postData) throws IOException {
        String base64Content = requestContext.get(SslContentKey).toString();
        String mchId = requestContext.get("mchId").toString();
        String key = mchId;
        CloseableHttpClient httpClient = OkHttpClientMap.get(key);
        if (httpClient == null) {
            SSLContext sslContext = initSSLContext(mchId, base64Content);
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                    null,//new String[]{"TLSv1"},
                    (String[]) null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            ;
            OkHttpClientMap.put(key, httpClient);
        }

        String result = null;
        try {
            HttpPost httpost = new HttpPost(url);
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(postData.toString(), "UTF-8"));
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpost);
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                EntityUtils.consume(entity);
                result = jsonStr;
            } catch (Exception ex) {
                ex.printStackTrace();
                return Pair.of(500, ex.getMessage());
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } finally {
            //httpClient.close();
        }

        return Pair.of(200, result);
        //return OkHttpUtil.execute(httpMethod, url, headers, postData);
    }

 

标签:http,请求,证书,微信,httpost,org,apache,import,String
From: https://www.cnblogs.com/zhshlimi/p/17612502.html

相关文章

  • Java HTTP请求封装的方法及实现
    JavaHTTP请求封装的方法及实现在Java开发中,我们经常需要与服务器进行数据交互,发送HTTP请求是其中常见的一种方式。为了简化开发过程,我们可以封装HTTP请求的方法,让调用者只需要关注业务逻辑而不用关心底层的细节实现。本文将介绍一种基于Java的HTTP请求封装方法及其实现。JavaHT......
  • Java Web Service Get请求使用指南
    JavaWebServiceGet请求使用指南在当今互联网时代,WebService已经成为了现代软件开发中不可或缺的一部分。而Java作为一种广泛使用的编程语言,自然也提供了丰富的工具和库来支持WebService的开发。本文将为大家介绍如何使用Java编程语言进行WebService的Get请求。JavaWebserv......
  • 微信小程序11 弹窗showToast,showLoading,showModal
    弹窗是相当常用的功能,在微信里用弹窗还是挺方便的。不同于我们写网页时,对于alert,confirm这些比较简陋的原生弹窗通常要引入第三方插件来美化,微信自带的弹窗效果还不错。放一个按钮,绑定showToast方法。<buttonbind:tap="showToast">点击弹窗1</button>Js方法通用showToast......
  • 微信公众号授权回调 vue网址中带#号的处理
    1、改变vue模式为history,小编没有试2、通过配置nginx实现   A、替换跳转网址中的#为其他字符串,例如我的    consturl=this._getUrl("https://open.weixin.qq.com/connect/oauth2/authorize",{appid:this.appId,......
  • 微信开发之自动同意好友的技术实现
    简要描述:同意添加好友请求URL:http://域名地址/acceptUser请求方式:POST请求头Headers:Content-Type:application/jsonAuthorization:login接口返回参数:参数名必选类型说明wId是string登录实例标识v1是stringv1(从消息回调中取)v2是stringv2(从消......
  • 软件工程造价师证书有效期到了过期了怎么办?
    软件工程造价师培训课程自推出以来,已经为社会各界培养了近万名软件工程造价核算人员,为信息化项目软件成本管理和控制发挥了积极作用。软件工程造价师证书有效期为3年。持证人应在证书期满前3个月内或期满后6个月内,按照北京软件造价联盟公布的软件工程造价师证书续证办......
  • k8s x509数字证书
    证书生成方式手动证书生成工具在使用客户端证书认证的场景下,可以通过easyrsa、openssl或cfssl等工具以手工方式生成证书。证书API可以通过certificates.k8s.ioAPI提供x509证书,用来做身份验证。证书和证书签名请求Kubernetes证书和信任包(trustbundle)API可以通过为Kub......
  • 微信小程序9 关于解密,前端解密
    我们之前在学习button时,可以通过调用getphonenumber获取手机号,但是获取后却发现是加密了的,那么如何解密呢,微信官方提供了相应的方法。 1.首先我们可以去下载官方提供的解密的js放在本地,推荐放到这个utils文件下。关键是这个RdWXBizDataCrypt.js文件,可以通过这个名字去百度。......
  • 微信小程序反编译
    微信小程序反编译工具准备1.模拟器(夜神,逍遥等都可以)2.模拟器里的微信3.模拟器里的RE管理工具,这个可以现在物理机里下载了apk文件,然后拖到模拟器里4.安装nodejs环境https://www.swvq.com/link/to?link=https://nodejs.org/en/5.反编译工具链接:https://pan.baidu.com/s......
  • vue中axios多次相同请求取消上一个请求
    apiimportaxiosfrom'axios';exportfunctiongetDataFlowGraph(id,that){returnrequest({requestBase,url:`/workflowMetadata/getDataFlowGraph/${id}`,method:'get',//设置canceltoken用于多次请求,中断上一次请求cancel......