Package com.wanhua.weixin.util;
Import Java.util.HashMap;
Import Java.util.Map;
Import Org.json.JSONObject;
Import Play.cache.Cache;
Import Play.i18n.Messages;
Import Com.alibaba.fastjson.JSON;
Import Com.wanhua.weixin.model.AccessToken;
/**
* Tool class to request address URL
*
* @author W_xfpenga 2015-1-5
*
*/
public class Urlutil {
/**
* Turn a long link into a short link
*
* @param longurl
* Long link
* @return return Short link
*/
public static string Longtoshorturl (String longurl) {
Set Debug Mode Flags
Boolean flag = true;
System.out.println ("--urlutil----longtoshorturl---flag---" + flag);
If debug mode, return the original long link
if (flag) {
return longurl;
}
A link that returns a long link to a short link when it is not in debug mode
The credential that gets to
String Access_token = Accesstoken.getaccesstoken ();
Request Address
String Requesturl = Wxconst.long_to_short_url + Access_token;
Request parameters
map<string, string> map = new hashmap<string, string> ();
Map.put ("Action", "Long2short");
Map.put ("Long_url", Longurl);
Request OUTPUT parameters
String outputstr = json.tojsonstring (map);
String shorturl = null;
try {
The result of the request returned
String result = Wxhttputil.msghttpsrequest (Requesturl, "POST", outputstr);
Convert returned results to JSON formatted data
Jsonobject resultobj = new Jsonobject (result);
The status code of the returned result
int code = resultobj.getint ("Errcode");
if (code = = 0) {
Returns the short link URL
Shorturl = resultobj.getstring ("Short_url");
Request successfully returned prompt message
System.out.println (Messages.get ("long_to_short_url_success") + result);
} else {
Request failed return prompt information
System.out.println (Messages.get ("long_to_short_url_fail") + result);
}
} catch (Exception e) {
E.printstacktrace ();
}
return shorturl;
}
}
Turn a long link into a short link