private void Stream(string hashTag) { logger.Info("Start Streaming Hash: {0}", hashTag); string resourceUrl = Twitter.STREAMING_API_V1_1 + "statuses/filter.json"; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(resourceUrl); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Timeout = -1; // This is the content var postData = new List<KeyValuePair<string, string>>(); postData.Add(new KeyValuePair<string, string>("track", hashTag)); // sign Request OAuthParameters oAuthParameters = new OAuthParameters(this.authorization, resourceUrl, postData, new List<KeyValuePair<string, string>>()); OauthHeader oauthHeader = new OauthHeader(oAuthParameters); webRequest.Headers.Add("Authorization", oauthHeader.ToString()); webRequest.AddPostData(postData); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); var webResponse = (HttpWebResponse)webRequest.GetResponse(); var responseStream = new StreamReader(webResponse.GetResponseStream(), encode); logger.Info("Stream Initialized. Entering listening mode..."); this.processor.SetStartTime(); //Read the stream. while (!this.cancel) { string jsonText = responseStream.ReadLine(); logger.Debug("Recieved from twitter:" + jsonText); this.processor.Process(jsonText); } this.processor.SetEndTime(); //Abort is needed or responseStream.Close() will hang. webRequest.Abort(); responseStream.Close(); webResponse.Close(); this.Canceled = true; }
private static OAuthParameters TestOAuthParameters() { KeyValuePair<string, string> body = new KeyValuePair<string, string>( "status", "Hello Ladies + Gentlemen, a signed OAuth request!"); KeyValuePair<string, string> urlParam = new KeyValuePair<string, string>("include_entities", "true"); // sample from twitter OauthAuthorization oauthAuthorization = new OauthAuthorization( "xvz1evFS4wEEPTGEFPHBog", "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw", "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb", "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE"); OAuthParameters oAuthParameters = new OAuthParameters( oauthAuthorization, "https://api.twitter.com/1/statuses/update.json", new List<KeyValuePair<string, string>>() { body }, new List<KeyValuePair<string, string>>() { urlParam }); // override with sample data. oAuthParameters.TimeStamp = "1318622958"; oAuthParameters.Nonce = "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg"; return oAuthParameters; }
public OauthHeader(OAuthParameters oAuthParameters) { this.parameters = oAuthParameters; }