private string OAuthPostRequest(string url, string postData) { string responseString = string.Empty; WebResponse response = null; try { Uri uri = new Uri(url); string outURL = string.Empty; string outQueryString = string.Empty; OAuthParameters p = new OAuthParameters(); p.ConsumerKey = m_oAuthConsumerKey; p.HttpMethod = HttpMethod.POST; p.ConsumerSecret = m_oAuthConsumerSecret; p.Nonce = GenerateOAuthNonce(); p.Status = postData; p.SignatureType = SignatureTypes.HMACSHA1; p.Timestamp = GenerateOAuthTimestamp(); p.Token = m_oAuthAccessToken; p.TokenSecret = m_oAuthAccessTokenSecret; p.Uri = new Uri(url); string sig = this.GenerateSignature(p, out outURL, out outQueryString); outQueryString += "&oauth_signature=" + Common.UrlEncode(sig); if (outQueryString.Length > 0) { outURL += "?"; } string newURL = outURL + outQueryString; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newURL); request.Method = "POST"; request.ServicePoint.Expect100Continue = false; request.ContentLength = 0; request.ContentType = "application/x-www-form-urlencoded"; //request.Headers.Add("content-length", postData.Length.ToString()); response = request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { responseString = reader.ReadToEnd(); } if (response.Headers["X-RateLimit-Limit"] != null) { int limit = int.Parse(response.Headers["X-RateLimit-Limit"]); int remaining = int.Parse(response.Headers["X-RateLimit-Remaining"]); long resetTime = long.Parse(response.Headers["X-RateLimit-Reset"]); m_rateLimit = new RateLimit(limit, remaining, resetTime); } } catch (Exception ex) { responseString = "***ERROR :: " + ex.Message; } return responseString; }
private RateLimit GetRateLimit(string response) { RateLimit rateLimit = new RateLimit(); if (!response.Contains("ERROR")) { try { XDocument document = XDocument.Parse(response, LoadOptions.None); XElement ele = document.Root; rateLimit.HourlyLimit = (ele.Element("hourly-limit") != null) ? int.Parse(ele.Element("hourly-limit").Value) : 0; rateLimit.RemainingHits = (ele.Element("remaining-hits") != null) ? int.Parse(ele.Element("remaining-hits").Value) : 0; rateLimit.ResetTime = (ele.Element("reset-time") != null) ? ele.Element("reset-time").Value : string.Empty; rateLimit.ResetTimeInSeconds = (ele.Element("reset-time-in-seconds") != null) ? Int64.Parse(ele.Element("reset-time-in-seconds").Value) : 0; } catch { } } else { ShowErrorMessage(response); } return rateLimit; }