private JsonObject QueryPublic(string a_sMethod, string props = null) { string address = string.Format("{0}/{1}/public/{2}", _url, _version, a_sMethod); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(address); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Method = "POST"; if (props != null) { using (var writer = new StreamWriter(webRequest.GetRequestStream())) { writer.Write(props); } } //Make the request try { //Wait for RateGate _rateGate.WaitToProceed(); using (WebResponse webResponse = webRequest.GetResponse()) { using (Stream str = webResponse.GetResponseStream()) { using (StreamReader sr = new StreamReader(str)) { return((JsonObject)JsonConvert.Import(sr)); } } } } catch (WebException wex) { using (HttpWebResponse response = (HttpWebResponse)wex.Response) { using (Stream str = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(str)) { if (response.StatusCode != HttpStatusCode.InternalServerError) { throw; } return((JsonObject)JsonConvert.Import(sr)); } } } } }
/// <summary> /// Limits the rate at which the sequence is enumerated. /// </summary> /// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">The <see cref="IEnumerable{T}"/> whose enumeration is to be rate limited.</param> /// <param name="count">The number of items in the sequence that are allowed to be processed per time unit.</param> /// <param name="timeUnit">Length of the time unit.</param> /// <returns>An <see cref="IEnumerable{T}"/> containing the elements of the source sequence.</returns> public static IEnumerable <T> LimitRate <T>(this IEnumerable <T> source, int count, TimeSpan timeUnit) { using (var rateGate = new RateGate(count, timeUnit)) { foreach (var item in source) { rateGate.WaitToProceed(); yield return(item); } } }
private JsonObject QueryPublic(string a_sMethod, string props = null) { string address = string.Format("{0}/{1}/public/{2}", _url, _version, a_sMethod); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(address); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Method = "POST"; lock (_queryLocker) { if (_proxies != null && _proxies.Count != 0 && _lastProxyNum < _webProxies.Count) { webRequest.Proxy = _webProxies[_lastProxyNum]; _lastProxyNum++; } if (webRequest.Proxy != null && webRequest.Proxy.Credentials == null && _webProxies != null && _lastProxyNum >= _webProxies.Count) { _lastProxyNum = 0; } try { if (props != null) { using (var writer = new StreamWriter(webRequest.GetRequestStream())) { writer.Write(props); } } } catch (Exception) { //Thread.Sleep(2000); if (props != null) { using (var writer = new StreamWriter(webRequest.GetRequestStream())) { writer.Write(props); } } } //Wait for RateGate _rateGate.WaitToProceed(); } //Make the request try { using (WebResponse webResponse = webRequest.GetResponse()) { using (Stream str = webResponse.GetResponseStream()) { using (StreamReader sr = new StreamReader(str)) { return((JsonObject)JsonConvert.Import(sr)); } } } } catch (WebException wex) { using (HttpWebResponse response = (HttpWebResponse)wex.Response) { using (Stream str = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(str)) { if (response.StatusCode != HttpStatusCode.InternalServerError) { throw; } return((JsonObject)JsonConvert.Import(sr)); } } } } }