GetTwitterResponseAsString ( String url, RequestStatistics requestStatistics, ReportProgressHandler reportProgressHandler, CheckCancellationPendingHandler checkCancellationPendingHandler ) { Debug.Assert(!String.IsNullOrEmpty(url)); Debug.Assert(requestStatistics != null); AssertValid(); Int32 iRateLimitPauses = 0; Int32 iInvalidJsonRepeats = 0; const Int32 MaximumInvalidJsonRepeats = 3; while (true) { // Add the required authorization information to the URL. // // Note: Don't do this outside the while (true) loop. The // authorization information includes a timestamp that will // probably expire if the code within the catch block pauses. oAuthTwitter oAuthTwitter = new oAuthTwitter( m_UserAgent, m_TimeoutMs); oAuthTwitter.Token = m_TwitterAccessToken; oAuthTwitter.TokenSecret = m_TwitterAccessTokenSecret; String sAuthorizedUrl, sAuthorizedPostData; oAuthTwitter.ConstructAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty, out sAuthorizedUrl, out sAuthorizedPostData); url = sAuthorizedUrl; Stream oStream = null; try { oStream = HttpSocialNetworkUtil.GetHttpWebResponseStreamWithRetries( url, HttpStatusCodesToFailImmediately, requestStatistics, m_UserAgent, m_TimeoutMs, reportProgressHandler, checkCancellationPendingHandler); return(GetTwitterResponseAsString(oStream)); } catch (InvalidJsonException oInvalidJsonException) { iInvalidJsonRepeats++; if (iInvalidJsonRepeats > MaximumInvalidJsonRepeats) { throw oInvalidJsonException; } HttpSocialNetworkUtil.ReportProgress(reportProgressHandler, "Received invalid JSON from Twitter. Trying again." ); } catch (WebException oWebException) { if ( !WebExceptionIsDueToRateLimit(oWebException) || iRateLimitPauses > 0 ) { throw; } // Twitter rate limits have kicked in. Pause and try again. iRateLimitPauses++; Int32 iRateLimitPauseMs = GetRateLimitPauseMs(oWebException); DateTime oWakeUpTime = DateTime.Now.AddMilliseconds( iRateLimitPauseMs); HttpSocialNetworkUtil.ReportProgress(reportProgressHandler, "Reached Twitter rate limits. Pausing until {0}." , oWakeUpTime.ToLongTimeString() ); // Don't pause in one large interval, which would prevent // cancellation. const Int32 SleepCycleDurationMs = 1000; Int32 iSleepCycles = (Int32)Math.Ceiling( (Double)iRateLimitPauseMs / SleepCycleDurationMs); for (Int32 i = 0; i < iSleepCycles; i++) { if (checkCancellationPendingHandler != null) { checkCancellationPendingHandler(); } System.Threading.Thread.Sleep(SleepCycleDurationMs); } } finally { if (oStream != null) { oStream.Close(); } } } }
GetTwitterResponseAsString ( String url, RequestStatistics requestStatistics, ReportProgressHandler reportProgressHandler, CheckCancellationPendingHandler checkCancellationPendingHandler ) { Debug.Assert( !String.IsNullOrEmpty(url) ); Debug.Assert(requestStatistics != null); AssertValid(); Int32 iRateLimitPauses = 0; while (true) { // Add the required authorization information to the URL. // // Note: Don't do this outside the while (true) loop. The // authorization information includes a timestamp that will // probably expire if the code within the catch block pauses. oAuthTwitter oAuthTwitter = new oAuthTwitter( m_UserAgent, m_TimeoutMs); oAuthTwitter.Token = m_TwitterAccessToken; oAuthTwitter.TokenSecret = m_TwitterAccessTokenSecret; String sAuthorizedUrl, sAuthorizedPostData; oAuthTwitter.ConstructAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty, out sAuthorizedUrl, out sAuthorizedPostData); url = sAuthorizedUrl; Stream oStream = null; try { oStream = HttpSocialNetworkUtil.GetHttpWebResponseStreamWithRetries( url, HttpStatusCodesToFailImmediately, requestStatistics, m_UserAgent, m_TimeoutMs, reportProgressHandler, checkCancellationPendingHandler); return ( new StreamReader(oStream).ReadToEnd() ); } catch (WebException oWebException) { if ( !WebExceptionIsDueToRateLimit(oWebException) || iRateLimitPauses > 0 ) { throw; } // Twitter rate limits have kicked in. Pause and try again. iRateLimitPauses++; Int32 iRateLimitPauseMs = GetRateLimitPauseMs(oWebException); DateTime oWakeUpTime = DateTime.Now.AddMilliseconds( iRateLimitPauseMs); if (reportProgressHandler != null) { reportProgressHandler( String.Format( "Reached Twitter rate limits. Pausing until {0}." , oWakeUpTime.ToLongTimeString() ) ); } // Don't pause in one large interval, which would prevent // cancellation. const Int32 SleepCycleDurationMs = 1000; Int32 iSleepCycles = (Int32)Math.Ceiling( (Double)iRateLimitPauseMs / SleepCycleDurationMs) ; for (Int32 i = 0; i < iSleepCycles; i++) { if (checkCancellationPendingHandler != null) { checkCancellationPendingHandler(); } System.Threading.Thread.Sleep(SleepCycleDurationMs); } } finally { if (oStream != null) { oStream.Close(); } } } }
AuthorizeIfRequested() { AssertValid(); switch (m_oTwitterAuthorizationControl.Status) { case TwitterAuthorizationStatus.NoTwitterAccount: // Delete any access token that exists. TwitterAccessToken.Delete(); return (true); case TwitterAuthorizationStatus.HasTwitterAccountAuthorized: return (true); case TwitterAuthorizationStatus.HasTwitterAccountNotAuthorized: TwitterAccessToken.Delete(); // Continue below. break; default: Debug.Assert(false); break; } // Get a Twitter request token. oAuthTwitter oTwitterAuth = new oAuthTwitter( HttpNetworkAnalyzerBase.UserAgent, HttpNetworkAnalyzerBase.HttpWebRequestTimeoutMs); Uri oAuthorizationUri = new Uri( oTwitterAuth.AuthorizationLinkGet() ); String sRequestToken = HttpUtility.ParseQueryString( oAuthorizationUri.Query)["oauth_token"]; Debug.Assert( !String.IsNullOrEmpty(sRequestToken) ); // Open the Twitter authorization page. Process.Start( oAuthorizationUri.ToString() ); // Tell the user that the Twitter authorization page has been opened in // a browser window, and get the PIN that Twitter provides after // authorization is complete. TwitterAuthorizingDialog oTwitterAuthorizingDialog = new TwitterAuthorizingDialog(); if (oTwitterAuthorizingDialog.ShowDialog( m_oTwitterAuthorizationControl.ParentForm) != DialogResult.OK) { return (false); } // Convert the request token to an access token. oTwitterAuth.Token = sRequestToken; oTwitterAuth.AccessTokenGet( sRequestToken, oTwitterAuthorizingDialog.Pin); Debug.Assert( !String.IsNullOrEmpty(oTwitterAuth.Token) ); Debug.Assert( !String.IsNullOrEmpty(oTwitterAuth.TokenSecret) ); // Save the access token. TwitterAccessToken oTwitterAccessToken = new TwitterAccessToken(); oTwitterAccessToken.Save(oTwitterAuth.Token, oTwitterAuth.TokenSecret); return (true); }