zoukankan      html  css  js  c++  java
  • 微信公众号长链接转短链接(long2short)接口返回40052错误码

    java中,使用apache的http相关接口,发送post请求,访问微信公众号长链接转短链接接口,好嘛,出问题了。(中间的都是灌水,结果在最底)

    返回了40052错误码,像这样:  

    {"errcode":40052,"errmsg":"invalid action name hint: [qSmxHa0039vr19]"}  

    ,出问题了不要紧,咱马上上官网查返回码说明,可是,mmp的,官网上居然没得,有图有真相。下图

    然后,球法,再来一句mmp,还是得自己找原因。还行,问了下度娘,说可能是你发送的post请求参数格式不对,别个不认识。

    行,咱就比对下参数嘛,我是用的之前自己封装的工具类。

    官网上是这样的:

    curl -d "{"action":"long2short","long_url":"http://wap.koudaitong.com/v2/showcase/goods?
    alias=128wi9shh&spm=h56083&redirect_count=1"}" "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN"

    我工具类源码是这样的:

     1 public static String doPost(String url, Map<String, String> params) throws Exception {
     2         String result;
     3         if (params == null || params.size() <= 0) {
     4             result = doGet(url);
     5         } else {
     6             HttpClient httpClient = HttpClients.createDefault();
     7             HttpPost httpPost = new HttpPost(url);
     8             //=======注意这里组装参数的方式
     9             List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
    10             Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
    11             while (iterator.hasNext()) {
    12                 Map.Entry<String, String> entry = iterator.next();
    13                 parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    14             }
    15             httpPost.setEntity(new UrlEncodedFormEntity(parameters, DEFAULT_CHARSET)); //UTF-8
    16             //=========================
    17             HttpResponse httpResponse = httpClient.execute(httpPost);
    18             HttpEntity httpEntity = httpResponse.getEntity();
    19             result = EntityUtils.toString(httpEntity, DEFAULT_CHARSET);
    20         }
    21         return result;
    22     }

    反正开始也看不出来,然后 貌似官网的参数是json字符串,好嘛,我改改我的代码试试,过程就不说了,最后如下:

     1 public static String doPostByJson(String url, Map<String, String> params) throws Exception {
     2         String result;
     3         HttpClient httpClient = HttpClients.createDefault();
     4         HttpPost httpPost = new HttpPost(url);
     5         String s = JSON.toJSONString(params);
     6         httpPost.setEntity(new StringEntity(s, DEFAULT_CHARSET));//以json形式发送
     7         HttpResponse httpResponse = httpClient.execute(httpPost);
     8         HttpEntity httpEntity = httpResponse.getEntity();
     9         result = EntityUtils.toString(httpEntity, DEFAULT_CHARSET);
    10         return result;
    11     }

    就是标红色的,把参数转换成json字符串发送就行了。嗯,就这么简单。。。。。。

  • 相关阅读:
    微信公众号支付JSAPI,提示:2支付缺少参数:appId
    Application对象
    Apache安装和文件配置
    Apache编译教程
    MS4W安装教程
    MapServer教程2
    Tomcat修改源码,重新编译
    MapServer教程
    QGIS SDK下载
    OpenStreetMap全球库
  • 原文地址:https://www.cnblogs.com/VweiweiyixiaoV/p/7661813.html
Copyright © 2011-2022 走看看