public GetProxyModel GetRandomProxy() { //Opvragen willekeurige proxy HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.getproxylist.com/proxy"); try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { //Lezen stream en omzetten naar StreamReader reader = new StreamReader(responseStream); string readresult = reader.ReadToEnd(); GetProxyModel result = JsonConvert.DeserializeObject <GetProxyModel>(readresult); return(result); } } catch (Exception ex) { Console.WriteLine("Foutboodschap: " + ex.Message); } return(null); }
//Start downloaden van een bestand private async Task DownloadFileAsync(string url, string naam) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; try { using (WebClient wc = new WebClient()) { wc.DownloadProgressChanged += Client_DownLoadProcessChanged; //Om tussentijdse feedback te kunnen geven. wc.DownloadFileCompleted += Client_DownloadProcessComplete; //Om feedback te kunnen geven bij voltooing //Make instance off class library DownloadEVERadioSessions downloadEVERadioSessionsClassLib = new DownloadEVERadioSessions(); if (UseProxy) //Use a proxy if the user wants to { GetProxyModel proxyinfo = downloadEVERadioSessionsClassLib.GetRandomProxy(); //Get a random proxy wc.Proxy = new WebProxy(proxyinfo.Ip, proxyinfo.Port); //Proxy instellen } //timer per download starten EVERadioSession evers = EVERadioSessions.Find(f => f.FileName == naam.Split('\\').Last()); evers.StopWatch.Start(); //Extra informatie per download bijhouden. evers.TimeStarted = DateTime.Now; await wc.DownloadFileTaskAsync(new Uri(url), naam); } } catch (Exception ex) { Console.WriteLine(ex.Message); if (ex.Message == "Unable to connect to the remote server") { FeedbackList.Add("Error, try again with Proxy enabled"); } else { FeedbackList.Add("Bestand: " + naam.Split('\\').Last() + " - Something went wrong, maybe this helps: " + ex.Message); } FeedbackColor = "Red"; } }