Java利用HttpClient发送请求生成支付二维码、查询支付状态
Java利⽤HttpClient发送请求⽣成⽀付⼆维码、查询⽀付状态⼀、HttpClient⼯具类
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.ity.UrlEncodedFormEntity;
import org.apache.hods.*;
import org.ssl.SSLConnectionSocketFactory;
import org.ssl.SSLContextBuilder;
import org.ssl.TrustStrategy;
import org.ity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.ssage.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import javax.ssl.SSLContext;
import java.io.IOException;
import CertificateException;
import X509Certificate;
ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**呼兰河传好词好句
* http请求客户端
*/
public class HttpClient {
private String url;
private Map<String, String> param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
public boolean isHttps(){
return isHttps;
}
public void setHttps(boolean isHttps){
this.isHttps = isHttps;
}
public String getXmlParam(){
return xmlParam;
}
public void setXmlParam(String xmlParam){
}
public HttpClient(String url, Map<String, String> param){
this.url = url;
this.param = param;
}
public HttpClient(String url){
this.url = url;
}
public void setParameter(Map<String, String> map){
param = map;
}
public void addParameter(String key, String value){
if(param == null)
param =new HashMap<String, String>();
param.put(key, value);
}
public void post()throws ClientProtocolException, IOException {
HttpPost http =new HttpPost(url);
setEntity(http);郑爽整容前后照片
execute(http);
}
public void put()throws ClientProtocolException, IOException {
HttpPut http =new HttpPut(url);
setEntity(http);
execute(http);
}
public void get()throws ClientProtocolException, IOException {
if(param != null){
StringBuilder url =new StringBuilder(this.url);
boolean isFirst =true;
for(String key : param.keySet()){
if(isFirst)
url.append("?");
else
url.append("&");
url.append(key).append("=").(key));
}
this.url = String();
}
HttpGet http =new HttpGet(url);
execute(http);
qq自定义皮肤图片
}
女朋友生日祝福语/**
* set http post,put param
*/
private void setEntity(HttpEntityEnclosingRequestBase http){
if(param != null){
List<NameValuePair> nvps =new LinkedList<NameValuePair>();
for(String key : param.keySet())
nvps.add(new BasicNameValuePair(key, (key)));// 参数
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));// 设置参数}
if(xmlParam != null){
http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
}
}
private void execute(HttpUriRequest http)throws ClientProtocolException,
IOException {
CloseableHttpClient httpClient = null;
try{
if(isHttps){
SSLContext sslContext =new SSLContextBuilder()
.loadTrustMaterial(null,new TrustStrategy(){
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf =new SSLConnectionSocketFactory(      sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf) .build();
}else{
httpClient = ateDefault();
}
CloseableHttpResponse response = ute(http);
try{
换驾驶证需要什么资料和流程
if(response != null){
StatusLine()!= null)
statusCode = StatusLine().getStatusCode();
HttpEntity entity = Entity();
// 响应内容
content = String(entity, Consts.UTF_8);
}
}finally{
response.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
httpClient.close();
}
}
public int getStatusCode(){
return statusCode;
}
public String getContent()throws ParseException, IOException {
return content;
}
}
⼆、⽣成⽀付⼆维码
public Map createNative(String orderNo){
try{
//1 根据订单号查询订单信息
QueryWrapper<Order> wrapper =new QueryWrapper<>();
wrapper.eq("order_no", orderNo);
Order order = One(wrapper);
//2 使⽤map设置⽣成⼆维码需要参数
Map m =new HashMap();
/
/1、设置⽀付参数
m.put("appid","wx74862e0dfcf6****");
m.put("mch_id","155895****");
m.put("nonce_str", ateNonceStr());
m.put("body", CourseTitle());
m.put("out_trade_no", orderNo);
m.put("total_fee", TotalFee().multiply(new BigDecimal("100")).longValue()+"");
m.put("spbill_create_ip","127.0.0.1");
m.put("notify_url","******/api/order/weixinPay/weixinNotify\n");
m.put("trade_type","NATIVE");
//3 发送httpclient请求传递参数xml格式⽀付提供固定的地址
HttpClient client =new HttpClient("h.weixin.qq/pay/unifiedorder");
//设置cml格式的参数
client.ateSignedXml(m,"T6m9iK73b0kn9g5v426MKfHQH7******"));            client.setHttps(true);
//执⾏请求发送
client.post();
//4 得到发送请求的返回结果
//返回的内容是xml格式将xml转化为map
String content = Content();
//将xml转为map
Map<String, String> resultMap = lToMap(content);
//最终返回数据的封装
Map map =new HashMap<>();
map.put("out_trade_no", orderNo);
map.put("course_id", CourseId());
map.put("total_fee", TotalFee());
map.put("result_code", ("result_code"));
map.put("code_url", ("code_url"));
return map;
}catch(Exception e){
throw new GuliException(20001,"⽣成⼆维码失败");
}
}
三、查询⽀付状态
public Map<String, String>queryPayStatus(String orderNo){
try{
//1、封装参数
Map m =new HashMap<>();
m.put("appid","wx74862e0dfcf6****");
m.put("mch_id","155895****");
m.put("out_trade_no", orderNo);
m.put("nonce_str", ateNonceStr());
//2、设置请求
HttpClient client =new HttpClient("h.weixin.qq/pay/orderquery");
client.ateSignedXml(m,"T6m9iK73b0kn9g5v426MKfHQH7******"));            client.setHttps(true);
client.post();
//3、返回第三⽅的数据
String xml = Content();
Map<String, String> resultMap = lToMap(xml);
//6、转成Map
//7、返回
return resultMap;
}catch(Exception e){
e.printStackTrace();
}
return null;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。