Pages

Tuesday, October 23, 2012

C# code for ADFLY API and it works for similar services

Do you want program to create dynamic short url from your application?? Then here is the class file written in C# programming language. This is very simple code and any one can modify and reuse this code. Also enhancement are appreciated if you have done on top of this code. This is specifically written and tested for adf.ly short url service. Just by modifying APP_ID and USERID you can use it for your url shortner account.

Here i provided full class file written in C#.NET technology. Below this class program, i have provided how to call this class. Calling this service is pretty much simpler and just two line needed. Use GenerateShortUrl function under try catch block, you can see any error occurred in class file as an exception; otherwise you will get short url successfully obtained from adf.ly website.

Code C#


using System.Net;
using System.IO;

 public class UrlShortService
    {
        private string _APIKEY;
        private string _UID;
        public enum AdvertType {Banner, FullPage};
        public enum Domain {Adf_ly,q_gs};
        private string  _AdvType;
        private string  _Domain;
        private string _UrlBaseAddress;
        private string _RequestURL;
        private string GetAdvType(AdvertType adv)
        {
            string tmpAdType;
            if (adv == AdvertType.FullPage)
                tmpAdType = "int";
            else
                tmpAdType = "banner";
            return tmpAdType;
        }
        public UrlShortService(string APPKEY, string UID, AdvertType AdvType, Domain DomainType)
        {
            _UrlBaseAddress = "http://api.adf.ly/api.php";
            _APIKEY = APPKEY;
            _UID = UID;
            _AdvType = GetAdvType(AdvType);
            _Domain = GetDomainName(DomainType);
            _RequestURL = string.Format("{0}?key={1}&uid={2}&advert_type={3}&domain={4}&url=", _UrlBaseAddress, _APIKEY, _UID, _AdvType, _Domain);
        }

        public UrlShortService(AdvertType AdvType)
        {
            _UrlBaseAddress = "http://api.adf.ly/api.php";
            _APIKEY = "1a01cb1aa861a0d11f293beba530e6a7"; //provide your appkey
            _UID = "2655514"; // provide your User ID
            _AdvType = GetAdvType(AdvType);
            _Domain = GetDomainName(Domain.Adf_ly);
            _RequestURL = string.Format("{0}?key={1}&uid={2}&advert_type={3}&domain={4}&url=", _UrlBaseAddress, _APIKEY, _UID, _AdvType, _Domain);
        }

        public UrlShortService()
        {
            _UrlBaseAddress = "http://api.adf.ly/api.php";
            _APIKEY = "1a01cb1aa861a0d11f293beba530e6a7";
            _UID = "2655514";
            _AdvType = GetAdvType(AdvertType.Banner);
            _Domain = GetDomainName(Domain.Adf_ly);
            _RequestURL = string.Format("{0}?key={1}&uid={2}&advert_type={3}&domain={4}&url=", _UrlBaseAddress, _APIKEY, _UID, _AdvType, _Domain);
        }

        
        public string GenerateShortUrl(string UrlName)
        {
            string Result;
            try
            {
                HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(_RequestURL+UrlName);
                HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

                Result = new StreamReader(httpResp.GetResponseStream()).ReadToEnd().Trim();

                if (Result.ToLower() == "error")
                {
                    throw new Exception("ShortUrl Response Error");
                }
            }
            catch (Exception err)
            {
                throw new Exception("ShortUrl Conversion Error: " + err.Message);
            }

            return Result;
        }        
        
        }

How to Use:


            UrlShortService urlshort = new UrlShortService();
            string ShortUrl = urlshort.GenerateShortUrl("http://intbuzz.blogspot.com");

ShortUrl contains value
 http://adf.ly/DxO9t

Please feel free to ask any doubts in your mind. Also provide comment if it is useful to you.