zoukankan      html  css  js  c++  java
  • 微信接口简介

    1、  了解HttpClient

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

    HttpClient有如下特点

    (1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)

    (2)支持自动跳转

    (3)支持 HTTPS 协议

    (4)支持代理服务器等

    微信公众号需要用到的就是(1)和(3),所以这里只演示(1)和(3)

    Get方法

    public void get() throws Exception {
    
        // (1) 创建HttpGet实例
    
        HttpGet get = new HttpGet("http://localhost:8080/iheima/getWithNoParam");
    
    
    
        // (2) 使用HttpClient发送get请求,获得返回结果HttpResponse
    
        HttpClient http = HttpClients.createDefault();
    
        HttpResponse response = http.execute(get);
    
    
    
        // (3) 读取返回结果
    
        if (response.getStatusLine().getStatusCode() == 200) {
    
            HttpEntity entity = response.getEntity();
    
    
    
            InputStream in = entity.getContent();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    
            String line = null;
    
            while ((line = reader.readLine()) != null) {
    
                System.out.println(line);
    
            }
    
            in.close();
    
            reader.close();
    
        }
    
    }

    Get带参数

    public void getWithParam() throws URISyntaxException, IOException {
    
        // (1)创建查询参数
    
    
    
        URI uri = new URIBuilder()
    
                .setScheme("http")
    
                .setHost("127.0.0.1")
    
                .setPort(8080)
    
                .setPath("/iheima/getWithParam")
    
                .setParameter("param", "ice")
    
                .build();
    
    
    
        // (2) 创建Get实例
    
        HttpGet get = new HttpGet(uri);
    
    
    
        // (3) 使用HttpClient发送get请求,获得返回结果HttpResponse
    
        HttpClient http = HttpClients.createDefault();
    
        HttpResponse response = http.execute(get);
    
    
    
        // (4) 读取返回结果
    
        if (response.getStatusLine().getStatusCode() == 200) {
    
            HttpEntity entity = response.getEntity();
    
    
    
            InputStream in = entity.getContent();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    
            String line = null;
    
            while ((line = reader.readLine()) != null) {
    
                System.out.println(line);
    
            }
    
            in.close();
    
            reader.close();
    
        }
    
    }
    

    Post方法

    public void post() throws Exception {
    
        // (1) 创建HttpPost实例
    
        HttpPost post = new HttpPost("http://localhost:8080/iheima/postWithNoParam");
    
    
    
        // (2) 使用HttpClient发送post请求,获得返回结果HttpResponse
    
        HttpClient http = HttpClients.createDefault();
    
        HttpResponse response = http.execute(post);
    
    
    
        // (3) 读取返回结果
    
        if (response.getStatusLine().getStatusCode() == 200) {
    
            HttpEntity entity = response.getEntity();
    
    
    
            InputStream in = entity.getContent();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    
            String line = null;
    
            while ((line = reader.readLine()) != null) {
    
                System.out.println(line);
    
            }
    
            in.close();
    
            reader.close();
    
        }
    
    }
    

    Post带参数

    public void postWithParam() throws IOException {
    
        // (1) Post请求
    
        HttpPost post = new HttpPost("http://localhost:8080/iheima/postWithParam");
    
    
    
        //添加参数
    
        List<NameValuePair> params = new ArrayList<NameValuePair>();
    
        params.add(new BasicNameValuePair("param", "post ice"));
    
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
    
    
    
        // (3) 发送请求
    
        HttpClient http = HttpClients.createDefault();
    
        HttpResponse response = http.execute(post);
    
    
    
        // (3) 读取返回结果
    
        if (response.getStatusLine().getStatusCode() == 200) {
    
            HttpEntity entity = response.getEntity();
    
    
    
            InputStream in = entity.getContent();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    
            String line = null;
    
            while ((line = reader.readLine()) != null) {
    
                System.out.println(line);
    
            }
    
            in.close();
    
            reader.close();
    
        }
    
    }

    2、  了解微信接口

    微信提供的接口基本上都是以SSL (Secure Sockets Layer 安全套接层)加密请求的。

    如获取access_token的接口地址为:

    以下为参数说明:

    参数

    是否必须

    说明

    grant_type

    获取access_token填写client_credential

    appid

    第三方用户唯一凭证

    secret

    第三方用户唯一凭证密钥,即appsecret

    正常情况下,微信会返回下述JSON数据包给公众号:

    {"access_token":"ACCESS_TOKEN","expires_in":7200}

    对照的参数表为:

    参数

    说明

    access_token

    获取到的凭证

    expires_in

    凭证有效时间,单位:秒

    以下为使用SSL加密方式用get的方式调用微信接口的方法:

    public void getSSL() throws Exception {
    
        // Trust own CA and all self-signed certs
    
        SSLContext sslcontext = SSLContexts.custom()
    
                .loadTrustMaterial(null,
    
                        new TrustStrategy() {
    
                            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {//信任所有
    
                                return true;
    
                            }
    
                        })
    
                .build();
    
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
    
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    
        try {
    
            HttpGet httpget = new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET");
    
            System.out.println("executing request " + httpget.getRequestLine());
    
            CloseableHttpResponse response = httpclient.execute(httpget);
    
            // (3) 读取返回结果
    
            if (response.getStatusLine().getStatusCode() == 200) {
    
                HttpEntity entity = response.getEntity();
    
                InputStream in = entity.getContent();
    
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    
                String line = null;
    
                while ((line = reader.readLine()) != null) {
    
                    System.out.println(line);
    
                }
    
                in.close();
    
                reader.close();
    
            }
    
            response.close();
    
        } finally {
    
            httpclient.close();
    
        }
    
    }
     
    最后附上maven项目所需要的依赖:
    <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.4</version>
    </dependency>
     
    注:微信各类详细接口明天再讲
     
     
    微信接口开发流程:
    1、在开发者首次提交验证申请时,微信服务器将发送GET请求到填写的URL上,并且带上四个参数(signature、timestamp、nonce、echostr),开发者通过对签名(即signature)的效验,来判断此条消息的真实性。

    参数

    描述

    signature

    微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

    timestamp

    时间戳

    nonce

    随机数

    echostr

    随机字符串

    加密/校验流程如下:
    1. 将token、timestamp、nonce三个参数进行字典序排序
    2. 将三个参数字符串拼接成一个字符串进行sha1加密
    3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
    第一次处理来自于微信和校验时,只需要要把echostr返回给来自于微信的请求即校验通过
     
     全局access_token:其他微信接口,需要通过基础支持中的“获取access_token”接口来获取到的普通access_token调用。
     
     授权access_token:通过OAuth2.0机制实现的,在用户授权给公众号后,公众号可以获取网页授权access_token,通过网页授权access_token可以进行授权后接口调用,如获取用户基本信息;
     
     用户open_id
    用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID,相对于一个公众号来说,openid是唯一的
     
     用户union_id:
    1、请注意,网页授权获取用户基本信息也遵循UnionID机制。即如果开发者有在多个公众号,或在公众号、移动应用之间统一用户帐号的需求,需要前往微信开放平台(open.weixin.qq.com)绑定公众号后,才可利用UnionID机制来满足上述需求。
    2、UnionID机制的作用说明:如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为同一用户,对同一个微信开放平台下的不同应用(移动应用、网站应用和公众帐号),unionid是相同的。
     
    微信接口列表
    1.    获取access_token
    2.    接收消息
      Ø  验证消息真实性:使用我们上面介绍的方法进行验证
      Ø  接收消息,
      Ø  接收事件:关注、取关、扫描二维码()、定位、点击自定义菜单(分为两类:获取信息和跳转)
    3.    发送消息
      Ø  自动回复
      Ø  客服(没研究)
      Ø  群发
      Ø  回复模板(一定格式的微信消息)
    4.    素材管理
    5.    用户管理
      Ø  设置备注名
      Ø  获取用户列表(分页获取,每页10000个,获取的是OpenID)
      Ø  获取用户信息(两种方式:UinionID和OpneID)
      Ø  授权登录(即授权获取用户信息)
    6.    自定义菜单
      Ø  自定义的菜单有两种类型:
        n   点击后以消息方式返回结果;click
        n   以链接形式形成跳转.view
      Ø  增删查
      Ø  有关事件
    7.    获取统计信息
    8.    JS-SDK(可以用微信扫下,体验下接口类型,不列举了╮(╯▽╰)╭)
    
    
      To do 是否可以转发到朋友圈
    9.    微信小店接口
    10.  多客服接口
    11.  微信支付
      Ø  刷卡支付
      Ø  公众号支付
      Ø  扫码支付
      Ø  App支付
      Ø  代金券或立减支付
      Ø  现金红包
      Ø  企业付款
     
     
    参考资料
    微信公众号文档:
    http://mp.weixin.qq.com/wiki/home/index.html
     
    微信开放平台文档:
    https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
     
    微信支付文档:
    http://pay.weixin.qq.com/wiki/doc/api/index.html
     
    测试帐号地址:
    http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
     
    支付开发教程(微信商户平台版):
    https://mp.weixin.qq.com/paymch/readtemplate?t=mp/business/course3_tmpl&lang=zh_CN
     
    方倍工作室:
    http://www.cnblogs.com/txw1958/
     
    封装好的微信框架:
    https://github.com/liyiorg/weixin-popular
    https://github.com/chanjarster/weixin-java-tools
     
    httpcomponents 文档:https://hc.apache.org/httpcomponents-client-4.4.x/tutorial/html/index.html
     
    作业,使用把前面的httpclient的例子跑起来
  • 相关阅读:
    关于ArcGis for javascrept查询ArcGis server图层信息的方式
    Elasticsearch教程之基础概念
    C# 将日期转换成中文格式
    .Net 自定义Excel模板导出数据
    JavaScript判断浏览器类型
    简单的json传送数据
    Oracle替换临时表空间
    Python多线程循环
    crontab执行不生效-【问题篇】
    Python将MySQL表数据写入excel
  • 原文地址:https://www.cnblogs.com/yingbing/p/4357378.html
Copyright © 2011-2022 走看看