///
///从一条新浪微博地址中获取这条微博的id///
/// 一条微博地址
/// 微博id
public static string GetIdFromUrl(stringurl)
{string mid =GetMidFromUrl(url);if (string.IsNullOrEmpty(mid))
{return string.Empty;
}else{returnMid2Id(mid);
}
}///
///从一条新浪微博地址中获取这条微博的mid///
/// 一条微博地址
/// 微博mid
public static string GetMidFromUrl(stringurl)
{if (string.IsNullOrEmpty(url))
{return "";
}if (url.IndexOf('?') != -1)
{
url= url.Substring(0, url.IndexOf('?'));
}
Regex reg= new Regex(@"^http://(e\.)?weibo\.com/[0-9a-zA-Z]+/(?[0-9a-zA-Z]+)$", RegexOptions.IgnoreCase);
Match match=reg.Match(url);if(match.Success)
{return match.Result("${id}");
}return "";
}///
///将新浪微博mid转换成id///
/// 微博mid
/// 返回微博的id
public static string Mid2Id(stringmid)
{string id = "";for (int i = mid.Length - 4; i > -4; i = i - 4) //从最后往前以4字节为一组读取URL字符
{int offset1 = i < 0 ? 0: i;int len = i < 0 ? mid.Length % 4 : 4;var str =mid.Substring(offset1, len);
str=Str62toInt(str);if (offset1 > 0) //若不是第一组,则不足7位补0
{while (str.Length < 7)
{
str= "0" +str;
}
}
id= str +id;
}returnid;
}///
///新浪微博id转换为mid///
/// 微博id
/// 返回微博的mid
public static string Id2Mid(stringid)
{string mid = "", strTemp;intstartIdex, len;for (var i = id.Length - 7; i > -7; i = i - 7) //从最后往前以7字节为一组读取mid
{
startIdex= i < 0 ? 0: i;
len= i < 0 ? id.Length % 7 : 7;
strTemp=id.Substring(startIdex, len);
mid= IntToStr62(Convert.ToInt32(strTemp)) +mid;
}returnmid;
}//62进制转成10进制
public static string Str62toInt(stringstr62)
{
Int64 i64= 0;for (int i = 0; i < str62.Length; i++)
{
Int64 Vi= (Int64)Math.Pow(62, (str62.Length - i - 1));char t =str62[i];
i64+= Vi *GetInt10(t.ToString());
}returni64.ToString();
}//10进制转成62进制
public static string IntToStr62(intint10)
{string s62 = "";int r = 0;while (int10 != 0)
{
r= int10 % 62;
s62= Get62key(r) +s62;
int10= int10 / 62;
}returns62;
}//62进制字典
private static string str62keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVTXYZ";//获取key对应的62进制整数
private static Int64 GetInt10(stringkey)
{returnstr62keys.IndexOf(key);
}//获取62进制整数对应的key
private static string Get62key(intint10)
{if (int10 < 0 || int10 > 61)return "";return str62keys.Substring(int10, 1);
}