⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 twitterservice.cs

📁 Some time ago, I stated in another article that I d take the idea of location broadcasting and devel
💻 CS
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using DeepCast.Location;
using DeepCast;

namespace DeepCast.Services
{
    /// <summary>
    /// Service for posting Lat/Long to Twitter
    /// </summary>
    internal class TwitterService : GeoService
    {
        private const string Google_Map_Uri = "http://maps.google.com/maps?f=q&hl=en&sll={0},{1}&ll={0},{1}&q={0},{1}";
        //private const string Twitter_Api_Uri = "http://twitter.com/statuses/update.json";
        private const string Twitter_Api_Uri = "http://twitter.com/statuses/update.xml";
        
        /// <summary>
        /// Initializes a new instance of the <see cref="TwitterService"/> class.
        /// </summary>
        public TwitterService()
        {
            // Twitter now dishes out http 417 error codes for all requests with the 
            // header value "100-Continue". This globally disables 100 continue header
            System.Net.ServicePointManager.Expect100Continue = false; 
        }

        /// <summary>
        /// Posts a tweet to twitter.  Requires using TinyUrl to create a google map link since Twitter
        /// truncates query string parameters after the first key value pair.
        /// </summary>
        /// <param name="location">The location.</param>
        internal override void Update(GeoLocation location)
        {
            try
            {
                if (!string.IsNullOrEmpty(Settings.TwitterEmail) && !string.IsNullOrEmpty(Settings.TwitterPassword))
                {

                    string googleMapLink = string.Format(Google_Map_Uri, location.Latitude, location.Longitude);
                    string tinyMapLink = TinyUrlService.GetTinyUrl(googleMapLink);
                    string place = string.Format("I'm here: {0} - 'L:{1}, {2}'",
                        new object[] { tinyMapLink, location.Latitude.ToStringNumeric(), location.Longitude.ToStringNumeric() });

                    // Encode credentials
                    string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Settings.TwitterEmail + ":" + Settings.TwitterPassword));
                    byte[] data = Encoding.ASCII.GetBytes("status=" + place);
                    
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Twitter_Api_Uri);
                    request.Method = "POST";
                    //request.ServicePoint.Expect100Continue = false;
                    request.Headers.Add("Authorization", "Basic " + user);
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;

                    Stream reqStream = request.GetRequestStream();
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();

                    //string data = string.Format("status={0}", place);

                    //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Twitter_Api_Uri);

                    //request.Credentials = new NetworkCredential(Settings.TwitterEmail, Settings.TwitterPassword);
                    //request.ContentType = "application/x-www-form-urlencoded";
                    //request.Method = "POST";
                    //request.AllowWriteStreamBuffering = true;

                    //byte[] bytes = Encoding.UTF8.GetBytes(data);

                    //request.ContentLength = bytes.Length;
                    //using (Stream requestStream = request.GetRequestStream())
                    //{
                    //    requestStream.Write(bytes, 0, bytes.Length);
                    //    requestStream.Flush();
                    //    requestStream.Close();

                    //    using (WebResponse response = request.GetResponse())
                    //    {
                    //        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    //        {
                    //            reader.ReadToEnd();
                    //        }
                    //    }
                    //}
                }
            }
            catch(Exception ex) 
            {
                string a = ex.Message;
            }
        }

        /// <summary>
        /// Gets the service's name.
        /// </summary>
        /// <value>The name.</value>
        internal override string Name
        {
            get { return "Twitter"; }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -