Turn a long link into a short link

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.