淺談Java代碼的 微信長鏈轉(zhuǎn)短鏈接口使用 post 請求封裝Json(實例)
更新時間:2017年07月21日 09:01:16 投稿:jingxian
下面小編就為大家?guī)硪黄獪\談Java代碼的 微信長鏈轉(zhuǎn)短鏈接口使用 post 請求封裝Json(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
廢話不多說,直接上代碼
String longUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + MpUtil.APPID + "&redirect_uri=" + MpUtil.HOMEPAGE + "/nweixinLoginPc.fo%3Frandomcode=" + randomcode + "&response_type=code&scope=snsapi_userinfo&state=account#wechat_redirect";//什么不重要 ,自己的長鏈 String accessToken = MpUtil.getAccessToken(MpUtil.APPID, MpUtil.APPSECRET); String shortUrl = null;//短連接地址,生成二維碼用,識別快 String httpUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token="+accessToken; //此處訪問微信短鏈方法 /** * 調(diào)用舉例 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" */ JSONObject jsonObject = new JSONObject(); jsonObject.put("action", "long2short"); jsonObject.put("long_url", longUrl); String transJson = jsonObject.toString()+httpUrl; RequestEntity se = new StringRequestEntity(transJson, "application/json", "UTF-8"); //微信返回的字符串 //成功 {"errcode":0,"errmsg":"ok","short_url":"http:\/\/w.url.cn\/s\/AvCo6Ih"} //失敗 {"errcode":40013,"errmsg":"invalid appid"} String resultsString = post(jsonObject,httpUrl); //封裝的post方法 String shortUrl = MpUtil.getJsonValue(resultsString, "short_url");//得到的短鏈 ?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849public static String post(JSONObject json,String URL) { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(URL); post.setHeader("Content-Type", "application/json"); post.addHeader("Authorization", "Basic YWRtaW46"); String result = ""; try { StringEntity s = new StringEntity(json.toString(), "utf-8"); s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(s); // 發(fā)送請求 HttpResponse httpResponse = client.execute(post); // 獲取響應輸入流 InputStream inStream = httpResponse.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader( inStream, "utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); result = strber.toString(); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return result; } else { result=""; } } catch (Exception e) { System.out.println("請求異常"); throw new RuntimeException(e); } return result; } 獲取參數(shù) 結(jié)果的 ?12345678910111213141516/** * 獲取json中的值 * @param json * @param key * @return */ public static String getJsonValue(String json, String key) { String value = ""; try { JSONObject jsonObj = new JSONObject(json); value = jsonObj.getString(key); } catch (Exception e) { value = ""; } return value; }
以上這篇淺談Java代碼的 微信長鏈轉(zhuǎn)短鏈接口使用 post 請求封裝Json(實例)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot數(shù)據(jù)層測試事務回滾的實現(xiàn)流程
這篇文章主要介紹了SpringBoot數(shù)據(jù)層測試事務回滾的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-10-10Spring?Boot多數(shù)據(jù)源事務@DSTransactional的使用詳解
本文主要介紹了Spring?Boot多數(shù)據(jù)源事務@DSTransactional的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06使用fastjson中的JSONPath處理json數(shù)據(jù)的方法
這篇文章主要介紹了使用fastjson中的JSONPath處理json數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04SpringBoot RestTemplate 簡單包裝解析
這篇文章主要介紹了SpringBoot RestTemplate 簡單包裝解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08myatisplus的saveOrUpdate的提交總是update問題
這篇文章主要介紹了myatisplus的saveOrUpdate的提交總是update問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11