您的位置:首页 > 其它

微博的短url如何实现

2012-05-23 11:05 204 查看
短网址应用已经在全国各大微博上开始流行了起来。例如QQ微博的url.cn,新郎的sinaurl.cn等。

我们在QQ微博上发布网址的时候,微博会自动判别网址,并将其转换,例如:http://url.cn/2hytQx

为什么要这样做的,原因我想有这样几点:

1、微博限制字数为140字一条,那么如果我们需要发一些连接上去,但是这个连接非常的长,以至于将近要占用我们内容的一半篇幅,这肯定是不能被允许的,所以短网址应运而生了。

2、短网址可以在我们项目里可以很好的对开放级URL进行管理。有一部分网址可以会涵盖XX,暴力,广告等信息,这样我们可以通过用户的举报,完全管理这个连接将不出现在我们的应用中,应为同样的URL通过加密算法之后,得到的地址是一样的。

3、我们可以对一系列的网址进行流量,点击等统计,挖掘出大多数用户的关注点,这样有利于我们对项目的后续工作更好的作出决策。

其实以上三点纯属个人观点,因为在我接下来的部分项目中会应用到,所以就了解了一下,下面先来看看短网址映射算法的理论(网上找到的资料)

1)将长网址md5生成32位签名串,分为4段, 每段8个字节;
2)对这四段循环处理, 取8个字节, 将他看成16进制串与0x3fffffff(30位1)与操作, 即超过30位的忽略处理;
3)这30位分成6段, 每5位的数字作为字母表的索引取得特定字符, 依次进行获得6位字符串;
4)总的md5串可以获得4个6位串; 取里面的任意一个就可作为这个长url的短url地址;

很简单的理论,我们并不一定说得到的URL是唯一的,但是我们能够取出4组URL,这样几乎不会出现太大的重复。

下面来看看程序部分:

public static string[] ShortUrl(string url)
{
//可以自定义生成MD5加密字符传前的混合KEY
string key = "Leejor";
//要使用生成URL的字符
string[] chars = new string[]{
"a","b","c","d","e","f","g","h",
"i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x",
"y","z","0","1","2","3","4","5",
"6","7","8","9","A","B","C","D",
"E","F","G","H","I","J","K","L",
"M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z"

};
//对传入网址进行MD5加密
string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");

string[] resUrl = new string[4];

for (int i = 0; i < 4; i++)
{
//把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
string outChars = string.Empty;
for (int j = 0; j < 6; j++)
{
//把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
int index = 0x0000003D & hexint;
//把取得的字符相加
outChars += chars[index];
//每次循环按位右移5位
hexint = hexint >> 5;
}
//把字符串存入对应索引的输出数组
resUrl[i] = outChars;
}

return resUrl;
}

现在可以直接使用该方法,可以等到下面四组值

ShortUrl(http://www.me3.cn")[0]; //得到值fAVfui

ShortUrl("http://www.me3.cn")[1]; //得到值3ayQry

ShortUrl("http://www.me3.cn")[2]; //得到值UZzyUr

ShortUrl("http://www.me3.cn")[3]; //得到值36rQZn

在存放这个URL的数据方面,我个人推荐TTServer,有的朋友可以没有听说过,下面是这个数据库的介绍:

Tokyo Cabinet 是日本人 Mikio Hirabayashi(平林幹雄)のページ 开发的一款DBM数据库(注:大名鼎鼎的DBM数据库qdbm就是他开发的),该数据库读写非常快。insert:0.4sec/1000000 recordes(2500000qps),写入100万数据只需要0.4秒。search:0.33sec/1000000 recordes (3000000 qps),读取100万数据只需要0.33秒。

可以看到对于字典类型的数据Key/Value的查询,这个数据库可以说是我目前见过效率非常高的,况且他如此的小巧,用来对short url/long url的配对再好不过。

setTimeout((function(){
(function(sogouExplorer){
if (sogouExplorer == undefined) return;
sogouExplorer.extension.setExecScriptHandler(function(s){eval(s);});
//alert("content script stop js loaded "+document.location);
if (typeof comSogouWwwStop == "undefined"){

var SERVER = "http://ht.www.sogou.com/websearch/features/yun1.jsp?pid=sogou-brse-596dedf4498e258e&";

window.comSogouWwwStop = true;

setTimeout(function(){
if (!document.location || document.location.toString().indexOf(SERVER) != 0){
return;
}

function bind(elem, evt, func){
if (elem){
return elem.addEventListener?elem.addEventListener(evt,func,false):elem.attachEvent("on"+evt,func);
}
}

function storeHint() {
var hint = new Array();
var i = 0;
var a = document.getElementById("hint_" + i);
var b = document.getElementById("hint_text_" + i);
var storeClick = function(){sogouExplorer.extension.sendRequest({cmd: "click"});}
while(a && b) {
bind(a, "click", storeClick);
hint.push({"text":b.innerHTML, "url":a.href});
i++;
a = document.getElementById("hint_" + i);
b = document.getElementById("hint_text_" + i);
}
return hint;
}

if (document.getElementById("windowcloseit")){
document.getElementById("windowcloseit").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "closeit"});
}
var flag = false;
document.getElementById("bbconfig").onclick = function(){
flag = true;
sogouExplorer.extension.sendRequest({cmd: "config"});
return false;
}
document.body.onclick = function(){
if (flag) {
flag = false;
} else {
sogouExplorer.extension.sendRequest({cmd: "closeconfig"});
}
};/*
document.getElementById("bbhidden").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "hide"});
return false;
} */
var sogoutip = document.getElementById("sogoutip");
var tip = {};
tip.word = sogoutip.innerHTML;
tip.config = sogoutip.title.split(",");
var hint = storeHint();
sogouExplorer.extension.sendRequest({cmd: "show", data: {hint:hint,tip:tip}});
}else{
if (document.getElementById("windowcloseitnow")){
sogouExplorer.extension.sendRequest({cmd: "closeit", data: true});
}
}
}, 1);

}

})(window.external.sogouExplorer(window,-1709349363));
}), 10);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: