微信公眾號長鏈接轉短鏈接(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字符串發送就行了。嗯,就這么簡單。。。。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM