Esempio n. 1
0
        public async static Task <string> Lookup(string link)
        {
            try
            {
                string longUrl;
                if (ShortToLongUrl.TryGetValue(link, out longUrl))
                {
                    return(longUrl);
                }

                var url     = "http://api.longurl.org/v2/expand?format=json&url=" + OAuth.UrlEncode(link);
                var request = WebRequestWrapper.Create(new Uri(url));
                request.UserAgent = "tweetz/5.0";
                request.Timeout   = 1500;
                using (var response = await request.GetResponseAsync())
                {
                    var serializer      = new DataContractJsonSerializer(typeof(LongUrlResponse));
                    var longUrlResponse = (LongUrlResponse)serializer.ReadObject(response.GetResponseStream());
                    if (string.IsNullOrWhiteSpace(longUrlResponse.LongUrl) == false)
                    {
                        if (ShortToLongUrl.Count > 1000)
                        {
                            ShortToLongUrl.Clear();
                        }
                        ShortToLongUrl.TryAdd(link, longUrlResponse.LongUrl);
                        return(longUrlResponse.LongUrl);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(link);
        }
Esempio n. 2
0
        private static string Shorten(string link)
        {
            var url = "http://is.gd/create.php?format=simple&url=" + OAuth.UrlEncode(link);

            using (var client = new WebClient())
            {
                var shortUrl = client.DownloadString(url);
                return(shortUrl);
            }
        }
Esempio n. 3
0
        private async static Task <string> WebRequest(string url, IEnumerable <string[]> parameters, bool post = false)
        {
            var oauth            = new OAuth();
            var nonce            = OAuth.Nonce();
            var timestamp        = OAuth.TimeStamp();
            var signature        = OAuth.Signature(post ? "POST" : "GET", url, nonce, timestamp, oauth.AccessToken, oauth.AccessTokenSecret, parameters);
            var authorizeHeader  = OAuth.AuthorizationHeader(nonce, timestamp, oauth.AccessToken, signature);
            var parameterStrings = parameters.Select(p => string.Format("{0}={1}", OAuth.UrlEncode(p[0]), OAuth.UrlEncode(p[1]))).ToList();

            if (!post)
            {
                url += "?" + string.Join("&", parameterStrings);
            }

            var request = WebRequestWrapper.Create(new Uri(url));

            request.Headers.Add("Authorization", authorizeHeader);
            request.Method = post ? "POST" : "GET";

            if (post)
            {
                Trace.TraceInformation(string.Join("&", parameterStrings));
                request.ContentType = "application/x-www-form-urlencoded";
                if (parameters != null)
                {
                    using (var requestStream = request.GetRequestStream())
                    {
                        WriteStream(requestStream, string.Join("&", parameterStrings));
                    }
                }
            }

            using (var response = await request.GetResponseAsync())
            {
                using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    var result = stream.ReadToEnd();
                    return(result);
                }
            }
        }