打通微信(一)

来源:互联网 发布:网络视频广告投放 编辑:程序博客网 时间:2024/04/30 10:19

目录

笔者接到一个企业微信公众号开发的项目,需要调用微信的api,翻看网络上的教程,大多难以有实质性的帮助,只能自己摸索摸索一番,其中调试时,着实花费了一番功夫,

首先申请一个免费的域名

直接百度上搜索 花生壳,下载 —-安装—-点击域名列表—–添加映射—–花6块钱,——将你的自己的IP和花生壳给的域名相绑定,端口填你的tomcat端口,

设置网页授权

登录公总号的后台,设置可信网页授权
这里写图片描述

设置按钮

在自定义菜单中点击添加
这里写图片描述

实际组件

需要在java 后台模拟http请求向微信端调用登录者的详细信息,
1.导包

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>${httpclient.version}</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>${httpmime.version}</version></dependency>
  1. 登录者点击你给的按钮,按钮后面的url会经过微信,微信来进行回调你,所以如果你需要本地调试,就得有一个独立的域名,

  2. 模拟http的get请求和post请求

  public HttpResultBean componentGetJson(String key, Map<String, String> urlParas, Object postData) {        String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token={access_token}";        if(StringUtil.isNullOrBlank(url)){            return null;        }        if(urlParas == null){            urlParas = new HashMap<>();        }        url = RequestUtil.buildUrl(url, urlParas);        HttpResultBean resultBean= HttpUtil.getData(url);        return resultBean;    }

处理url中的参数问题

   public String buildUrl(String url,Map  urlparas){ String fieldStart = "\\{";        String fieldEnd = "\\}";        if (map == null) {            return format;        }        String regex = fieldStart + "([^}]+)" + fieldEnd;        Set<String> fields = getCustomField(format, fieldStart, fieldEnd);        String result = format;        for (String field: fields){            String newVal = map.get(field);            if(newVal == null){                newVal = "";            }            result = result.replaceAll(fieldStart + field + fieldEnd, newVal);        }return result;}
public static HttpResultBean getData(String url, String path) {        CloseableHttpClient client = HttpClients.createDefault();        HttpGet get = new HttpGet(url);        CloseableHttpResponse response = null;        HttpResultBean result = new HttpResultBean();        try {            response = client.execute(get);            StatusLine status = response.getStatusLine();            result.setStatus(status.getStatusCode());            HttpEntity entity = response.getEntity();            String content = "";  content = EntityUtils.toString(entity, "UTF-8");            result.setContent(content);            result.setContentType(entity.getContentType().getValue());            Header encodingHeader = entity.getContentEncoding();            if (encodingHeader != null) {                result.setContentEncoding(encodingHeader.getValue());            }            result.setContentLength(entity.getContentLength());        } catch (IOException e) {            return null;        } finally {            if (response != null) {                try {                    response.close();                } catch (IOException e) {                }            }        }        return result;    }
原创粉丝点击