微信给的demo中HttpsRequest笔者使用的时候不好使,索性只能以此为基础进行改造。
改造后的HttpsRequest如下:支持代理

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;import com.tencent.service.IServiceRequest;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;/*** */public class HttpsRequests implements IServiceRequest
{// 连接超时时间,默认10秒private int socketTimeout = 10000;// 传输超时时间,默认30秒private int connectTimeout = 30000;// HTTP请求器private DefaultHttpClient httpClient;public HttpsRequests() throws Exception{KeyStore keyStore = KeyStore.getInstance("PKCS12");// 根据规则查找商户对应证书FileInputStream instream = new FileInputStream(new File(Configure.getCertLocalPath()));// 加载本地的证书进行https加密传输try{// 证书密码默认为mchIdkeyStore.load(instream, Configure.getCertPassword().toCharArray());// 设置证书密码}catch (Exception e){throw new RuntimeException("init throw Exception", e);}finally{try{instream.close();}catch (IOException e){}}SchemeRegistry registry = new SchemeRegistry();registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));// 证书密码默认为mchIdSSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, Configure.getCertPassword());socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);Scheme sch = new Scheme("https", socketFactory, 443);registry.register(sch);// 根据默认超时限制初始化requestConfigHttpParams params = new BasicHttpParams();params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);params.setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);//      //TODO 设置代理,生产环境需要去掉//      HttpHost proxy = new HttpHost("代理地址", 8080);//      CredentialsProvider credsProvider = new BasicCredentialsProvider();//      //TODO user\pwd改为代理用户密码//      UsernamePasswordCredentials creds = new UsernamePasswordCredentials("用户名", "密码");//      credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);//      httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);//      httpClient.setCredentialsProvider(credsProvider);}/*** 通过Https往API post xml数据** @param url*            API地址* @param xmlObj*            要提交的XML数据对象* @return API回包的实际数据* @throws IOException* @throws KeyStoreException* @throws UnrecoverableKeyException* @throws NoSuchAlgorithmException* @throws KeyManagementException*/public String sendPost(String url, Object xmlObj, int flag){String result = null;HttpPost httpPost = new HttpPost(url);// 将要提交给API的数据对象转换成XML格式数据Post给APIString postDataXML = xmlObj.toString();if (flag == 0){XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));//将要提交给API的数据对象转换成XML格式数据Post给APIpostDataXML = xStreamForRequestPostData.toXML(xmlObj);}try{// 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别StringEntity postEntity = new StringEntity(postDataXML, "UTF-8");httpPost.addHeader("Content-Type", "text/xml");httpPost.setEntity(postEntity);HttpResponse response = ((HttpClient) httpClient).execute(httpPost);int statusCode = response.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){Header locationHeader = response.getFirstHeader("location");String location = null;if (locationHeader != null){location = locationHeader.getValue();int index = location.indexOf("#");String new_s = location.substring(0, index) + location.substring(index + 16);return sendPost(new_s, xmlObj, flag);// 用跳转后的页面重新请求。}}HttpEntity entity = response.getEntity();result = EntityUtils.toString(entity, "UTF-8");}catch (Exception e){throw new RuntimeException("http get throw Exception", e);}finally{httpPost.abort();}return result;}public static void main(String[] args) throws Exception{HttpsRequests hr = new HttpsRequests();String r = hr.sendPost("", "");System.out.println(r);}
}
public interface IServiceRequest
{//Service依赖的底层https请求器必须实现这么一个接口public String sendPost(String api_url, Object xmlObj, int flag) throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException;

请求微信api(支持代理)相关推荐

  1. v3 微信api 请求微信_企业微信API使用基本教程

    在企业微信创建自建应用 1.登录企业微信后台,在"应用管理>自建"中点击"创建应用",填写应用信息创建. API配置表参数值获取 1.corpid:企业I ...

  2. PHP微信支付 curl请求https://api.mch.weixin.qq.com/pay/unifiedorder 返回空的解决方案

    PHP微信支付 curl请求https://api.mch.weixin.qq.com/pay/unifiedorder 返回空的解决方案 这个问题是我在生成微信支付二维码的时候碰到的,起初代码都是正 ...

  3. 服务器请求微信后台(api.weixin.qq.com)过慢处理

    问题描述:服务器请求微信后台(api.weixin.qq.com)的测试结果,每次请求都需要4,5秒甚至更慢. 2016.12.16 16:31:57 start:1481877117.4148 re ...

  4. 微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益

    大家好这一款微群人脉小程序系统以前也有发过 那么今天所发的这款呢是经过优化后的版本 之前的版本有很多用户反馈说登录后跳转到一个广告界面这个很不好会流失用户 所以呢小编就把它优化了一下,该版本目前的情况 ...

  5. 企业付款到零钱微信API请求报错:error:0909006C:PEM routines:get_name:no start line

    背景 对接企业付款到零钱,微信api请求时需要用到证书(node环境使用pem格式) 微信API:[微信支付]付款开发者文档 已经用微信的\WXCertUtil工具在本地生成了证书文件 问题所在: 证 ...

  6. 微信入口绑定,微信事件处理,微信API全部操作

    微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中. 微信支付.微信红包.微信卡券.微信小店. 1. index.php <?php include_once 'lib.inc.ph ...

  7. 总结的一些微信API接口

    本文给大家介绍的是个人总结的一些微信API接口,包括微信支付.微信红包.微信卡券.微信小店等,十分的全面,有需要的小伙伴可以参考下. 1. [代码]index.php <?php include ...

  8. 亚马逊 开发者api 调用_关于微信API:常用微信API文档整理

    微信公众平台消息接口为开发者提供了一种新的消息处理方式.微信公众平台消息接口为开发者提供与用户进行消息交互的能力.对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器会使用HT ...

  9. android高仿微信拍摄,Android 仿微信视频拍摄 支持触摸拍摄 长按拍摄

    JCamera This is Android CameraActivity,Imitation WeChat Camera Android 仿微信视频拍摄 支持触摸拍摄 长按拍摄,采用camera2 ...

最新文章

  1. Mahout分步式程序开发 聚类Kmeans
  2. Java实现AES和RSA算法
  3. 中药ppi网络图太杂乱_太杂乱了吗? 这是您的iPhone,iPad,Android或台式机的15张简约壁纸...
  4. 如何用代码对repeating section控件新增Item(InfoPath)
  5. PyQt5 GUI Programming With Python 3.6 (一)
  6. [转载] 柯受良-柯受良飞跃黄河
  7. LM324运放器应用实例
  8. SSM框架整合步骤思路及案例分析
  9. 爬虫练习生|爬虫前奏(爬虫介绍、工具的安装、爬虫分类、爬虫须知)
  10. 什么是GPU云服务器,有哪些优势,适用于什么场景?
  11. android游戏后台运行点击桌面游戏图标游戏重启的解决方法
  12. Windows下使用GetOpt函数使用
  13. css 实现简单的镂空渐变文字效果
  14. MIPIDSI转EDP芯片,东芝TC358867XBG芯片说明
  15. iOS 应用跳转到AppStore评分 可内置跳转
  16. 输出一个由*组成的三角形图案_三种由555芯片组成的输出电路
  17. 数学中鲜为人知的定理!
  18. 【详解】计算机视觉算法导读篇
  19. 办公计算机主机配置方案,高性能办公家用十代i5-10400核显装机电脑主机配置方案...
  20. 首届中国CDO大调研,CDO神秘面纱即将揭开...

热门文章

  1. Photoshop学习(二十二):创建木板纹理及凸起
  2. OS X各版本原厂系统镜像校验值索引
  3. Android之指南针学习
  4. 出厂校准的Gravity: 一氧化碳传感器工作原理和安装位置,调校标准说明
  5. 华为eNSP生成树基础实验配置
  6. 长安马自达CX-5减价增配 为您提供更多选择
  7. L1-047 装睡 (10分)
  8. SVG画扇形,可以改变数量、扇形角度、扇形方向
  9. layui select下拉菜单联动
  10. 智能手环产品分析(资料来源网上,自己总结)