public int SendHttp(string IP, string USER, string PASS) { if (dataToSend.Count() > 0) { WebClient webClient = new WebClientWithTimeout(); try { webClient.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(USER + ":" + PASS))); webClient.Credentials = new NetworkCredential(USER, PASS); string dds = string.Format("http://{0}:{1}/{2}?mac={3}&id={4}", tbServerIP.Text, tbPort.Text, tbResource.Text, GetMacAddress(), dataToSend[0].ToString().Replace("\n", "").Replace("\r", "")); //string dds = string.Format("http://{0}:{1}/{2}?mac={3}&id={4}", tbServerIP.Text, tbPort.Text, tbResource.Text, GetMacAddress(), dataToSend[0].ToString().Replace("\n", "").Replace("\r", "").Replace("-", "")); rtbLog.AppendText(dds + Environment.NewLine); //Catturo la response del servizio //byte[] xmlResponse = webClient.DownloadData(new Uri(dds)); //string s = System.Text.Encoding.Default.GetString(xmlResponse, 0, xmlResponse.Length); dataToSend.Clear(); //dataToSend.RemoveAt(0); } catch (Exception exc) { Console.WriteLine(exc.Message); webClient.Dispose(); System.Threading.Thread.Sleep(1000); }; } ; return(0); }
public void DownloadRemoteFile(string url, string destination) { try { using (WebClient wc = new WebClientWithTimeout()) { wc.Proxy = null; wc.DownloadFileCompleted += (s, e) => { Console.Write("100%"); Console.WriteLine(); Utils.Log($"Downloaded {url} to {destination}"); counter = 0; }; wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback); Stream stream = wc.OpenRead(url); // Size manipulations string size; long bytes = Convert.ToInt64(wc.ResponseHeaders["Content-Length"]); double mb = Math.Round((bytes / 1024f) / 1024f, 2); if (mb <= 0.00f) { size = Math.Round((bytes / 1024f), 2).ToString() + "KB"; } else { size = mb.ToString() + "MB"; } stream.Close(); Utils.Log($"Downloading {Path.GetFileName(destination)}: {size}..."); wc.DownloadFileTaskAsync(new Uri(url), destination).Wait(); wc.Dispose(); } } catch (WebException we) { Utils.Log(we.Message, Utils.MsgType.error); } }
private void _CloseWebClient() { _webClient.CancelAsync(); _webClient.Dispose(); }