7,774
社区成员




private static string GetShortenUrl(string longUrl)
{
const string urlformatter = "http://api.bit.ly/v3/shorten?login=stonepeter&apiKey=R_602a98ce2ef8feed660747065c1d7bae&longUrl={0}&format=xml";
var bitlyRequest = string.Format(urlformatter, UrlEncode(longUrl));
var xmlresult = WebRequest(Method.GET, bitlyRequest, null);
var xml = new XmlDocument();
xml.LoadXml(xmlresult);
string shortenUrl = xml.GetElementsByTagName("url")[0].InnerText;
return shortenUrl;
}
/// <summary>
/// Replace all Urls with shorten one
/// </summary>
/// <param name="orginal">The orginal text string</param>
/// <returns>If there are urls replace it with bit.ly shorten name</returns>
public static string ReplaceUrlWithShorten(string orginal)
{
Regex urlRegex = new Regex(@"http://[^\s]*");
MatchCollection matches = urlRegex.Matches(orginal);
string result = orginal;
foreach (Match match in matches)
{
string matchvalue = match.Value;
result = result.Replace(matchvalue, GetShortenUrl(matchvalue));
}
return result;
}