服务器之家

服务器之家 > 正文

浅谈Java代码的 微信长链转短链接口使用 post 请求封装Json(实例)

时间:2020-12-04 09:21     来源/作者:Java教程网

废话不多说,直接上代码

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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; //此处访问微信短链方法
        
        /**
         * 调用举例
            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);                // 发送请求           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;       }
 
 
获取参数 结果的
?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代码的 微信长链转短链接口使用 post 请求封装Json(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部