示例#1
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);
                }
            }
        }
示例#2
0
        public static void User(CancellationToken cancelationToken)
        {
            Task.Run(() =>
            {
                while (cancelationToken.IsCancellationRequested == false)
                {
                    var delay = Task.Delay(30 * 1000, cancelationToken);
                    delay.Wait(cancelationToken);
                    if (delay.IsCanceled || delay.IsFaulted)
                    {
                        break;
                    }

                    Trace.TraceInformation("{ Start Twitter User Stream }");
                    const string url    = "https://userstream.twitter.com/1.1/user.json";
                    var oauth           = new OAuth();
                    var nonce           = OAuth.Nonce();
                    var timestamp       = OAuth.TimeStamp();
                    var signature       = OAuth.Signature("GET", url, nonce, timestamp, oauth.AccessToken, oauth.AccessTokenSecret, null);
                    var authorizeHeader = OAuth.AuthorizationHeader(nonce, timestamp, oauth.AccessToken, signature);

                    var request = WebRequestWrapper.Create(new Uri(url));
                    request.Headers.Add("Authorization", authorizeHeader);

                    try
                    {
                        using (var response = request.GetResponse())
                            using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                            {
                                stream.BaseStream.ReadTimeout = 60 * 1000;
                                while (true)
                                {
                                    var json = stream.ReadLine();
                                    if (json == null)
                                    {
                                        Trace.TraceInformation("{ null }");
                                        break;
                                    }
                                    if (cancelationToken.IsCancellationRequested)
                                    {
                                        break;
                                    }
                                    Trace.TraceInformation(string.IsNullOrWhiteSpace(json) ? "{ Blankline }" : json);

                                    var serializer = new JavaScriptSerializer();
                                    var reply      = serializer.Deserialize <Dictionary <string, object> >(json);
                                    if (reply != null && reply.ContainsKey("user"))
                                    {
                                        Trace.TraceInformation("{ tweet identified }");
                                        var statuses = Status.ParseJson("[" + json + "]");
                                        Application.Current.Dispatcher.InvokeAsync
                                            (() => UpdateStatusHomeTimelineCommand.Command.Execute(statuses, Application.Current.MainWindow));
                                    }
                                }
                            }
                    }

                    catch (WebException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (ArgumentNullException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (ArgumentException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (InvalidOperationException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (IOException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }
                }

                Trace.TraceInformation("{ Stream task ends }");
            }, cancelationToken);
        }