URL short address compression algorithm-short URL mapping __ algorithm

Source: Internet
Author: User
Tags md5 stringbuffer

Microblog short Address principle parsing (Java implementation)

one way to do this is to invoke a third-party interface that provides a short-address service to generate. Generally they provide interfaces or call packages.

Such as:

How to call Baidu short URL api? Http://www.baidu.com/search/dwz.html (can be called without authorization)

Sina short URL API interface (updated March 15)

Sina updated long URL to short URL official API document: Http://open.weibo.com/wiki/2/short_url/shorten

Call Address: Https://api.weibo.com/2/short_url/shorten.json

Request Method: GET

Request parameter: Access_token: Get Url_long after authorization: Long address to be converted

return Result:

{"
    URLs": [
        {
            "url_short": "Http://t.cn/h4DwT1",
            "Url_long": "http://finance.sina.com.cn/",
            " Type ': 0,
            ' result ': ' True '
        }
    ]
}

Note: Access_token is required for each use, and access_token is required to be authorized after login. If you want to do a short URL conversion app, you certainly can't ask users to log in to Weibo, authorize, and then use your features so that the user experience is too poor. You can use the server side with a test account to obtain Access_token, so that users do not need to login to the micro-blog authorization, users can directly enter a long URL, to obtain the short translated URL.

For information on how to authorize access tokens, refer to this post: http://www.cnblogs.com/e241138/archive/2013/03/15/sina-weibo-oauth-access_token.html


The second method is implemented by itself, can be set up on a separate server to provide short URL mapping service.

(

There is a key point. Whether the short URL can be restored directly by the algorithm. If you cannot restore directly through the algorithm:

With storage (whether it's db, Cache, NoSQL, or whatever), it's not the focus of what algorithm to use. )

Recently, the project needs to use the short URL (shorturl) algorithm, so in the online search, found that there is a C # algorithm, there is. NET algorithm, there is a PHP algorithm, is not found in the Java version of the short URL (shorturl) algorithm, it is depressed. At the same time also found a lot of netizens in the post for help, how to achieve the Java version of the short URL (shorturl) algorithm. Simply conquered, over boots, refer to the online version of the popular PHP short URL (shorturl) algorithm:

According to their own understanding, Java implementation of the short URL (shorturl) algorithm. (\ (^o^)/yes. I'm a real tough man. )

First to the nonsense, is in other people's posts to see, mainly to let everyone know about the short URL (shorturl).

Nowadays, the application of short URL has already become popular in the national Weibo. such as QQ Weibo url.cn, groom's t.cn and so on.

When we publish the website on Sina Weibo, Weibo will automatically discriminate the URL and convert it, for example: Http://t.cn/hrYnr0. Why do you do this, why I want to have these points:

1, micro-Bo limit the number of words to 140, then if we need to send some connection up, but this connection is very long, so that nearly occupy half of our content, this must not be allowed, so the short URL came into being.

2, the short URL can be in our project can be very good to the open-level URL management. Some Web sites can cover sex, violence, advertising and other information, so that we can through the user's report, the full management of this connection will not appear in our application, should be the same URL through the encryption algorithm, the same address is obtained.

3, we can be a series of Web site traffic, click and other statistics, mining out the majority of users focus, so that it is conducive to our project follow-up work better decision-making.

In fact, the above three is purely personal point of view, because in my next part of the project will be applied to, so I learned a bit, the following first to look at the short URL mapping algorithm theory (online Find the information):

① the long URL with the MD5 algorithm to generate 32-bit signature string, divided into 4 segments, 8 characters per paragraph;

② the 4-stage loop processing, take 8 characters per paragraph, and consider him 16 binary string and 0X3FFFFFFF (30 bit 1) bit and operation, more than 30 bits of ignoring processing;

③ the 30 bits obtained in each section are divided into 6 segments, each 5 digits as an index of the alphabet to obtain a specific character, sequentially to obtain a 6-bit string;

④ such a MD5 string can get 4 6-bit strings, whichever one inside can be used as the short URL address of this long URL.

Very simple theory, we do not necessarily say that the URL is unique, but we are able to take out 4 sets of URLs, so there is almost no too much repetition.

First of all, please understand how to use MD5 to encrypt strings in Java to get a 32-bit encrypted string, the following is the Java MD5 algorithm I have encapsulated:[Java]  View Plain copy private final static string[] hexdigits = {           "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",            "8",  "9",  "a",  "B",  "C",  "D",  "E ", " F "};           public static String  Bytearraytohexstring (byte[] b) {           stringbuffer  resultsb = new stringbuffer ();            for  (int i = 0; i < b.length; i++) {                resultsb.append (byteToHexString (b[i));            }            return rEsultsb.tostring ();       }           Private static string bytetohexstring (byte b) {            int n = b;           if   (n < 0)                 n = 256 + n;           int d1 =  n / 16;           int d2 = n  % 16;           return hexdigits[d1] +  hexDigits[d2];       }           Public static string md5encode (string origin) {            string resultstring = null;                try {                                        resultstring=new string (origin);                    MessageDigest md =  Messagedigest.getinstance ("MD5");                                        resultstring.trim ();  

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.