Long and short connecting HTTP connection switching interface (API)

What is the long connection?

HTTP1.1 protocol specifies the default web address to maintain a long connection (HTTP persistent connection, also translated as persistent connections), so get to the web server links can only be connected to the original length.

What is the short connection?

Short connection (short-url), the name suggests is in the form of relatively short links, many of my friends are no longer strange, very common especially in the micro-blog application, for example, when we Tencent, Sina Weibo microblogging sometimes hair long URL connection, but because of restrictions microblogging only 140 words, so the microblogging automatically put your long hair into a short connection to the converter is connected. Undeniably in place to limit the number of words microblogging and SMS reminders to use a short URL, it is really a good program.

Long connections and short connection conversion interface

Sina short link interface: http://lnurl.cn/sina/short-api?link=http://www.baidu.com

Micro-channel short link interfaces: http://lnurl.cn/weixin/short?link=http://www.baidu.com


Interface Instructions:

1, online use

The API address "http://www.baidu.com"  parts into their long connection, and then go to copy the complete address can open a browser and paste generated. (In a nutshell is the replacement link back url)

2, the request interface

If you to larger than one of a conversion too cumbersome, require batch production, it may be generated for a request to the API interface program, requesting the following example.

 

PHP request example:

$url = 'http://www.baidu.com';
$api_url = 'http://lnurl.cn/sina/short-api?url_long=http://www.baidu.com;
$short_url = file_get_contents($api_url);
echo $short_url;

Java request example:

public static void main(String path[]) throws Exception {
URL u = new URL("http://lnurl.cn/sina/short-api?url_long=http://www.baidu.com");
InputStream in = u.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte buf[] = new byte[1024];
int read = 0;
while ((read = in .read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if ( in != null) {
in .close();
}
}
byte b[] = out.toByteArray();
System.out.println(new String(b, "utf-8"));
}


Python sample request:

import urllib, urllib2, sys
host = 'http://lnurl.cn'
path = '/sina/short-api'
method = 'GET'
querys = 'url_long=http%3A%2F%2Fwww.baidu.com'
bodys = {}
url = host + path + '?' + querys
request = urllib2.Request(url)
response = urllib2.urlopen(request)
content = response.read()
if (content):
print(content)

Precautions:

1, when calling API interface, simply "http://www.baidu.com" replaced the need to shorten the long link.

2, the interface supports links with parameters, but it should be noted that when the & symbol appears in the link, use 26% instead of (or use the url encoded), otherwise the parameters may be lost.

3, replace the link, must be based on http (s): // at the beginning, this may result in failure to generate or generate short URL short URL to access the original site can not jump.

 

common problem:

1, long after link translation parameters why the end is lost?

A: Because long link contains special characters, you need to url encoding before use interface generation.

2, the interface does not return results, what is the situation?

A: Sometimes the interface returns the data will be delayed, which generates a timeout failure does not return, it will not return result; or because the original link was closed.

3, valid for generating short-connection is how long? There is no limit visits?

A: The resulting short connection (t.cn & url.cn) is permanent, there is no limit clicks, you can rest assured that use.

 

Algorithm theory

A algorithm

1) The length of the URL string md5 generates 32-bit signature, divided into four segments, each one byte (i.e. 8 bits);

2) these four loop processing, take 4 bytes (32 bits), as he hexadecimal string and 0x3fffffff (30 bit 1) operation, i.e., more than 30 ignores the process;

3) 30 which is divided into 6 segments, each five-digit number as an index to obtain a particular alphabet character strings sequentially performed to obtain 6;

4) The total sequence obtained md5 four 6-bit string; can be taken arbitrarily as the inside of a short length url url addresses;

Algorithms two

The numbers and characters do some combination of the mapping, we can generate a unique character string, such as 62 combinations is aaaaa9, the first composition 63 is aaaaba, reuse shuffling algorithm, the original string storage disrupted, then the corresponding combination position in the string will be a combination of disordered.

The length of the URL stored in the database, taking returned id, find the corresponding character string, for example, returns the ID 1, then the corresponding character string is a combination of the above BBB, empathy ID is 2, the string combination BBA, and so on duplicate may appear after repeated may appear later, until it reaches 62 kinds of combinations, so if above 62 characters, any combination of characters take 6 to a string of words, your data stock of 500 million.

Guess you like

Origin blog.51cto.com/14637231/2459551