欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-19 19:37:52
废话不多说,直接上代码 string longurl = "https://open.weixin.qq.com/connect/oauth2/authorize...

废话不多说,直接上代码

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(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。