The emergence and prevalence of short links have benefited from the development of social networks. Short links can be easily transmitted over the network, avoiding exceeding the character limit, making it easier for the sharing address to count the access information of this address.
The following is Sina Weibo API short chain interface Description: http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2#.E7.9F.AD.E9.93.BE
We are most concerned about converting a long link into a short link, while other content is not concerned.
Open Long, chain to short chain content.
The request parameters include source, access_token, and url_long. The OAuth authorization method is used, and the source does not need to be filled in, otherwise, source is the AppKey of the application. OAuth authorization is used. access_token is required and the content is obtained after OAuth authorization. Otherwise, it is not required. Url_long is the long chain to be converted and is required. In combination with the above content, the typical formats for requesting this interface are as follows: OAuth authorization and non-OAuth authorization:
Https://api.weibo.com/2/short_url/shorten.json? Source = AppKey & url_long = NeedConvertURL
Https://api.weibo.com/2/short_url/shorten.json? Access_token = OAuthContent & url_long = NeedConvertURL
Research on OAuth authorization method read the document http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E
The non-authorization method only requires an AppKey and a long chain to be converted. You can obtain a large number of appkeys from Google. If you do not have your own applications and do not want to create them, one advantage of these appkeys is that the number of users is very large and the Access Frequency of interfaces is limited, it is equivalent to no limit.
Iphone Sina Weibo client App Key: 5786724301
IPad Sina client App Key: 2849184197
Google. Nexus wave client App Key: 1206405345
App Key: 202088835
Weico App Key: 211160679
With the AppKey, test the following to obtain the short link:
Https://api.weibo.com/2/short_url/shorten.json? Source = 5786724301 & url_long = http://www.baidu.com
Https://api.weibo.com/2/short_url/shorten.json? Source = 5786724301 & url_long = http://www.baidu.com & url_long = http://www.sina.com.cn
Result:
{"Urls": [{"result": true, "url_short": "http://t.cn/h5mwx", "url_long": "http://www.baidu.com", "type": 25}]}
{"Urls ":[
{"Result": true, "url_short": "http://t.cn/h5mwx", "url_long": "http://www.baidu.com", "type": 25 },
{"Result": true, "url_short": "http://t.cn/h5myh", "url_long": "http://www.sina.com.cn", "type": 25}
]}
Url_short in the returned result is what we want.
The following describes how to implement the short link service.
So the short link principle is very simple: Get a string corresponding to the long chain through a "method", splice it with the short domain name, and parse the original long chain when accessing this address, then jump.
To provide a short link, you must first have a domain name that is short enough. Obviously, the meaning of converting http://www.sina.com.cn into http://myshorturl.com/xysmfdis not big. We can see that the domain name used by Sina short link is t.cn, which is really short enough (the Domain Name of the short link service provided by Netease is 126.am ), therefore, if you want to provide short link services for others, you must first apply for a domain name that is short enough.
The next step is to convert the incoming long chain into a result similar to h5mwx through some "methods". Then, when accessing http://t.cn/h5mwx, find the corresponding long chain of h5mwx.
Most of the short links found on the Internet are implemented in the following article. Http://www.cnblogs.com/zhanghaoh/archive/2012/12/24/2831264.html
1 package com. bjdata. test; 2 3 import java. security. messageDigest; 4 import java. util. random; 5 6 7 public class ShortUrlTest {8 public static void main (String [] args) {9 String sLongUrl = "http://www.51bi.com/bbs/_t_278433840/"; // original link 10 System. out. println ("long link:" + sLongUrl); 11 String [] aResult = sLongUrl (sLongUrl ); // generates 4 6-bit strings 12 // print the result 13 for (int I = 0; I <aResult. length; I ++) {14 System. out. println ("[" + I + "]:" + aResult [I]); 15} 16 Random random = new Random (); 17 int j = random. nextInt (4); // a random number of less than 4 18 System. out. println ("short link:" + aResult [j]); // randomly fetch a short link as 19} 20 21 public static String [] Invalid url (String url) {22 // you can customize the hybrid KEY23 String key = "test" before MD5 encrypted characters are passed in "; 24 // use the 25 String [] chars = new String [] {"a", "B", "c", "d", "e ", "f", "g", "h", 26 "I", "j", "k", "l", "m", "n ", "o", "p", "q", "r", "s", "t", 27 "u", "v", "w ", "x", "y", "z", "0", "1", "2", "3", "4", "5 ", 28 "6", "7", "8", "9", "A", "B", "C", "D", "E ", "F", "G", "H", 29 "I", "J", "K", "L", "M", "N ", "O", "P", "Q", "R", "S", "T", 30 "U", "V", "W ", "X", "Y", "Z" 31 32}; 33 // MD5 encryption for the incoming url 34 String hex = md5ByHex (key + url ); 35 36 String [] resUrl = new String [4]; 37 for (int I = 0; I <4; I ++) {38 39 // perform bitwise operations on 8-bit hexadecimal pairs and 0x3FFFFFFF according to a group of octal characters. 40 String sTempSubString = hex. substring (I * 8, I * 8 + 8); 41 42 // here the long type is used for conversion, because Inteper. parseInt () can only process 31 bits. The first part is the symbol bit. If long is not used, it will cross the line 43 long lHexLong = 0x3FFFFFFF & Long. parseLong (sTempSubString, 16); 44 String outChars = ""; 45 for (int j = 0; j <6; j ++) {46 // bitwise AND operation are performed on the obtained value and 0x0000003D to obtain the character array chars index 47 long index = 0x0000003D & lHexLong; 48 // Add the obtained characters to 49 outChars + = chars [(int) index]; 50 // shifts the value of 5-digit 51 lHexLong = lHexLong> 5 each cycle; 52} 53 // Save the string to the output array of the corresponding index 54 resUrl [I] = outChars; 55} 56 return resUrl; 57} 58/** 59 * MD5 encryption (32-bit capital) 60 * @ param src61 * @ return62 */63 public static String md5ByHex (String src) {64 try {65 MessageDigest md = MessageDigest. getInstance ("MD5"); 66 byte [] B = src. getBytes (); 67 md. reset (); 68 md. update (B); 69 byte [] hash = md. digest (); 70 String hs = ""; 71 String stmp = ""; 72 for (int I = 0; I
This algorithm is worth improving:
- It uses 0x0000003D to convert the code into a binary value of 111101, indicating 62, if it is used for computation, it will block the number of the second bit as 1 when the binary representation is used, thus shielding nearly half of the content (because either 1 is 0, therefore, the shielding 1 is equivalent to a half cut, and 30 of the 0-61 values are viewed.) In fact, it is a 32-digit system.
- MD5 encryption results are divided into eight-bit groups, and finally four short addresses are generated. The probability of duplication is greatly increased. In 32-bit, 8-bit, one group, four groups, then randomly select a group. The probability of the same result is much higher than that of the 32-bit MD5 result duplication.
For the above two problems, I think it is possible to use a 64-in system. If the calculated result is 62 or 63, the random value in an array is used to solve the problem caused by 0x0000003D; does the second request provide four short addresses for re-selection in case of repeated requests?
I hope that the bloggers who read this blog can give me some ideas, it would be better to provide Google's short address or Sina's short address implementation method (the short address they provide seems to be 4-7 characters in size, it won't be a sequence converted into a 62 hexadecimal format, right ?!).
(In a blog post, I saw the blogger say that he saw the algorithm C # on the Internet and translated it into Java. I still appreciate it. After all, people think about it, write your own things. However, for such a piece of content, many "original" blog posts are not famous for their origins, nor do they have their own ideas ...... Ah ......)