Theme: default | dark | nature

Twitter

Note that you will have to sign up with Twitter and Bit.ly to get these credentials below. In your web.config file, make sure to define the following keys to your appSettings node:

    <add key="Bitly.Login" value="yourlogin" />
    <add key="Bitly.ApiKey" value="R_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
    <add key="Twitter.Username" value="yourusername" />
    <add key="Twitter.Password" value="yourpassword" />

These are the utility methods that you can use in your own code.

    private static readonly string BITLY_URL = "http://api.bit.ly/shorten?version=2.0.1&longUrl={0}&format=xml&login={1}&apiKey={2}";
    private static readonly string BITLY_LOGIN = ConfigurationManager.AppSettings["Bitly.Login"];
    private static readonly string BITLY_APIKEY = ConfigurationManager.AppSettings["Bitly.ApiKey"];

    private static readonly string TWITTER_URL = "http://twitter.com/statuses/update.xml";
    private static readonly string TWITTER_USERNAME = ConfigurationManager.AppSettings["Twitter.Username"];
    private static readonly string TWITTER_PASSWORD = ConfigurationManager.AppSettings["Twitter.Password"];
    
    public static string ShortenUrl(string url)
    {
        try
        {
            string result = string.Empty;

            XmlTextReader reader = new XmlTextReader(string.Format(BITLY_URL, url, BITLY_LOGIN, BITLY_APIKEY));
            while (reader.Read() && string.IsNullOrEmpty(result))
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "shortUrl")
                {
                    reader.Read();
                    result = reader.Value;
                }
            }

            return result;
        }
        catch
        {
            return url;
        }
    }
    
    public static string SetTwitterStatus(string status)
    {
        try
        {
            string post = string.Format("status={0}", HttpUtility.UrlEncode(status));
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(post);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(TWITTER_URL);
            
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(TWITTER_USERNAME + ":" + TWITTER_PASSWORD)));
            request.Method = "POST";
            request.ServicePoint.Expect100Continue = false;
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Your Twitter Bot";            
            request.ContentLength = bytes.Length;

            using (Stream writeStream = request.GetRequestStream())
            {
                writeStream.Write(bytes, 0, bytes.Length);
            }

            string result = string.Empty;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }

            return result;
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

Contact us today! Free consulting with no obligation.