Exemplo n.º 1
0
        public static async Task <TeknarProxyResult <T> > DeserializeJSON <T>(string url, string uploadData = null, bool useProxyCache = true)
        {
            try
            {
                if (!isStarted)
                {
                    Start();
                }
                #region Use Proxy Cache
                //if (useProxyCache)
                //{
                //    var cacheFileFullPath = Path.Combine(CacheDir, cacheFileName);
                //    var fileName = getSafeFleNameFromUrl(url);
                //    using (ZipArchive cacheZip = ZipFile.OpenRead(cacheFileFullPath))
                //    {
                //        var entry = cacheZip.GetEntry(fileName);
                //        if (entry != null)
                //        {
                //            try
                //            {
                //                using (var reader = new StreamReader(entry.Open()))
                //                {
                //                    return new TeknarProxyResult<T>
                //                    {
                //                        Url = url,
                //                        stringResult = "Proxy Cache Used",
                //                        Result = JsonConvert.DeserializeObject<T>(await reader.ReadToEndAsync().ConfigureAwait(false))
                //                    };
                //                }

                //            }
                //            finally
                //            {
                //            }
                //        }
                //    }
                //}
                #endregion
                int tryCount = 0;
                TeknarProxyResult <string> result = new TeknarProxyResult <string>
                {
                    Error = new Exception()
                };
                while (MaxTryCount > tryCount++ && result.hasException() && !result.isNotFoundError())
                {
                    result = await getJSONString(url, (new CancellationTokenSource(requestTimeout)).Token, uploadData).ConfigureAwait(false);
                }
                return(new TeknarProxyResult <T>
                {
                    Error = (tryCount > MaxTryCount) ?
                            new TeknarMultipleConnectionError(
                        string.Format("Maksimum deneme sayısı [{0}] aşıldı.", MaxTryCount),
                        new TeknarProxyResult <string>[] { result }) :
                            result.Error,
                    Proxy = result.Proxy,
                    Url = url,
                    stringResult = result.stringResult,
                    Result = (tryCount > MaxTryCount) || result.Error != null ? default(T) : JsonConvert.DeserializeObject <T>(result.stringResult)
                });
            }
            catch (Exception ex)
            {
                return(new TeknarProxyResult <T>
                {
                    Url = url,
                    Error = ex
                });
            }
        }
Exemplo n.º 2
0
        public static async Task <TeknarProxyResult <string> > getJSONString(string url, CancellationToken ct, string uploadData = null)
        {
            await ssl.WaitAsync().ConfigureAwait(false);

            TeknarProxyResult <string> result = new TeknarProxyResult <string>
            {
                Url = url
            };

            try
            {
                TeknarProxy proxy;
                if (!proxyList.TryGetValue((await selectProxy())._key, out proxy))
                {
                    ct.ThrowIfCancellationRequested();
                }
                result.Proxy = proxy;
                proxy.increaseCount();
                Stopwatch stw = new Stopwatch();
                using (WebClient wbc = new WebClient())
                {
                    wbc.Encoding = Encoding.UTF8;
                    wbc.Proxy    = new WebProxy(proxy.ip, proxy.port);
                    try
                    {
                        stw.Start();
                        ct.Register(wbc.CancelAsync);
                        if (string.IsNullOrWhiteSpace(uploadData))
                        {
                            result.stringResult = await wbc.DownloadStringTaskAsync(url).ConfigureAwait(false);
                        }
                        else
                        {
                            wbc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                            result.stringResult = await wbc.UploadStringTaskAsync(new Uri(url), "POST", uploadData).ConfigureAwait(false);
                        }
                        if (ct.IsCancellationRequested && result.stringResult == null)
                        {
                            ct.ThrowIfCancellationRequested();
                        }
                        stw.Stop();
                        proxy.updatePheromone((result.stringResult ?? string.Empty).Length, (long)stw.Elapsed.TotalMilliseconds);
                        if (string.IsNullOrWhiteSpace(result.stringResult))
                        {
                            throw new Exception("Hiçbir sunuç dönmedi!");
                        }
                    }
                    catch (Exception e)
                    {
                        stw.Stop();
                        proxy.updatePheromone((result.stringResult ?? string.Empty).Length, (long)stw.Elapsed.TotalMilliseconds);
                        result.Error = e;
                    }
                    //if (result.stringResult != null)
                    //{
                    //    try
                    //    {
                    //        await cacheResult(url, result.stringResult).ConfigureAwait(false);
                    //    }
                    //    finally
                    //    {
                    //    }
                    //}
                }
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }
            ssl.Release();
            return(result);
        }