Asp. NET implements the short link address program through the third party website bitly the practical skill

Source: Internet
Author: User

Weibo for a long time, perhaps I did not notice, now pay special attention to that micro-blog pictures link and URL link, always give me not understand but also so short feeling, anyway is not my upload address, in fact, here using a short link service function, here take out the sun.

Here is a look at Taobao is a product page, the link address is like this, but now in the micro Bo into the red circle table name that look, it seems that you do not understand, and you click After or to your original page. Here is the use of the short link program services, including pictures and so on, you can shorten the URL length.

Bitly is the world's most popular short link service, and has been free to open all features. Click to visit the official website bitly.com

The following is a description of the service usage steps:

(1) to the official website registration to obtain the corresponding Apikey

(2) to the setting page to get the key value

Because only after the registration to use its corresponding services, so here first register a user

(3) Download bitly Service Pack

Download the address below: http://urlshortenerhelper.codeplex.com/

(4) Create a new project, I use VS2010 MVC3 project here

After new, add the external reference package to the VS project, that is, install Urlshortener, the file you just downloaded

(5) Install Urlshortener in the package Manager Console command, where I install the urlshortener.0.9.1

After you enter the Install-package urlshortener command, the following indicates that the installation was successful!

(6) to the top after all OK, the next is coding things, the code is very simple.

After the installation is successful, there will be a package in the project project packages, which is a few DLL files, and then add two DLLs to the project.

(7) Code display

URL conversion must be set before user and just get key value

UrlShortener.Settings.UserName = "Qingyuan";

UrlShortener.Settings.ApiKey = "r_0daabdbac0fd2b58f097da9f5a1f7917";

The application code is as follows

<p>
@{
 String url = Urlshortener.shorten ("http://www.cnblogs.com/qingyuan/");
 String image = Urlshortener.shorten ("http://images.cnblogs.com/cnblogs_com/qingyuan/242728/r_qingyuan.jpg");
 string localhost = Urlshortener.shorten ("http://127.0.0.1/index.html");
}

<div>
 <label> show:</label>
 <span>
 <a href= "@url" > @url </a>
 </span>
</div><br/>
<div>
 <label> picture Short link:</label>
 < span>
 
 </span>
</div><br/>
<div>
 <label> Native Address:</label>
 <span>
 <a href= "@localhost" > @localhost </a>
 </span>
</div><br/></p> 

Show the effect of the following chart

This achieves the effect of short links, a lot shorter than the previous URL, and the URL has been confused.

Here are a few of the issues you've encountered:

(1) If you do not set username or Apikey, the following problems will occur

(2) If the value of Apikey is incorrect, the following problems will occur

Update get short link algorithm reference code

static void Main (string[] args) {string url = ' http://www.cnblogs.com/qingyuan/asdf/asdf/11/24/UrlShortener.html ';
 String[] results = shorturl (URL);
 foreach (string item in results) {System.Console.WriteLine (item);  } public static string[] Shorturl (string 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"};
 String value = UserMd5 (URL); System.Console.WriteLine ("MD5 string after encryption:" +value+ \ n String Length "+value.)
 Length); String[] results = new String[value.
 LENGTH/8]; for (int i = 0; i < value. LENGTH/8; i++) {String tempresult = value.
 Substring (i*8,8); Long Lhexlong = 0x3fffffff & long.
 Parse (Tempresult); String OutchAR = "";
  for (int j = 0; J < 6; J +) {Long index = 0x0000003d & Lhexlong;
  Outchar + = chars[(int) index];
 Lhexlong = Lhexlong >> 5;
 } Results[i] = Outchar;
return results;
 static string UserMd5 (String str) {string key = ' Qingyuan ';
 string cl = Str+key;
 string pwd = ""; MD5 MD5 = MD5.
 Create ();
 Byte[] s = Md5.computehash (Encoding.UTF8.GetBytes (str));
 for (int i = 0; i < s.length i++) {pwd = pwd + s[i];

return pwd; 

 }

Above this article asp.net through the third party website bitly realization Short link Address program is the small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

Related Article

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.