Example #1
0
        private void SendRequestData(string URL, string method, bool showProgress)
        {
            clientSocket = new TcpClient();
            Uri URI = new Uri(URL);

            clientSocket.Connect(URI.Host, URI.Port);
            requestHeaders.Add("Host", URI.Host);
            byte[] request = GetRequestHeaders(method + " " + URI.PathAndQuery + " HTTP/1.1");
            clientSocket.Client.Send(request);
            if (postStream == null)
            {
                return;
            }
            byte[] buffer = new byte[10245];
            int    count2 = 0;
            Stream sm     = clientSocket.GetStream();

            postStream.Position = 0L;
            UploadEventArgs e = default(UploadEventArgs);

            e.totalBytes = postStream.Length;
            Stopwatch timer = new Stopwatch();

            timer.Start();
            while (!isCanceled)
            {
                count2 = postStream.Read(buffer, 0, buffer.Length);
                sm.Write(buffer, 0, count2);
                if (showProgress)
                {
                    e.bytesSent   += count2;
                    e.sendProgress = (double)e.bytesSent / (double)e.totalBytes;
                    double t2 = timer.ElapsedMilliseconds / 1000;
                    t2          = ((t2 <= 0.0) ? 1.0 : t2);
                    e.sendSpeed = (double)e.bytesSent / t2;
                    this.UploadProgressChanged?.Invoke(this, e);
                }
                if (count2 <= 0)
                {
                    break;
                }
            }
            timer.Stop();
            postStream.Close();
            postStream = null;
        }
Example #2
0
        private void SendRequestData(string URL, string method, bool showProgress)
        {
            isSSL        = URL.StartsWith("https");
            clientSocket = new TcpClient();
            Uri URI = new Uri(URL);

            clientSocket.Connect(URI.Host, isSSL ? 443 : URI.Port);
            requestHeaders.Add("Host", URI.Host);

            Stream networkStream = clientSocket.GetStream();

            workStream = isSSL ? new SslStream(networkStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null) : networkStream;
            byte[] request = GetRequestHeaders(method + " " + URI.PathAndQuery + " HTTP/1.1");

            if (!isSSL)
            {
                clientSocket.Client.Send(request);
            }
            else
            {
                ((SslStream)workStream).AuthenticateAsClient(URI.Host);
                workStream.Write(request, 0, request.Length);
                workStream.Flush();
            }

            if (postStream == null)
            {
                return;
            }
            byte[] buffer = new byte[10245];
            int    count2 = 0;

            postStream.Position = 0L;
            UploadEventArgs e = default(UploadEventArgs);

            e.totalBytes = postStream.Length;
            Stopwatch timer = new Stopwatch();

            timer.Start();
            while (!isCanceled)
            {
                count2 = postStream.Read(buffer, 0, buffer.Length);
                workStream.Write(buffer, 0, count2);
                if (showProgress)
                {
                    e.bytesSent   += count2;
                    e.sendProgress = (double)e.bytesSent / (double)e.totalBytes;
                    double t2 = timer.ElapsedMilliseconds / 1000;
                    t2          = ((t2 <= 0.0) ? 1.0 : t2);
                    e.sendSpeed = (double)e.bytesSent / t2;
                    this.UploadProgressChanged?.Invoke(this, e);
                }
                if (count2 <= 0)
                {
                    break;
                }
            }
            timer.Stop();
            postStream.Close();
            postStream = null;
        }