How to download a Github release file using Webclient on . NET 3.5?

Asked

Viewed 39 times

0

After my question of Create Zip files and unzip using . NET 3.5 I have a problem. It’s just that before I was able to download my file in a Github release, but then it didn’t work anymore.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.IO.Compression;
using System.Reflection;

namespace IEIPInstaller
{
    public partial class Main : Form
    {
        private string temp = Path.Combine(Path.GetTempPath(), "IEIPInst");
        private string localProg = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        public Main()
        {
            if (Directory.Exists(temp))
            {
                if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
                {
                    File.Delete(temp + "\\IEIPv0.2-alpha-Windows.zip");
                }
                Directory.Delete(temp);
            }
            if(File.Exists(localProg + "\\IEIPv0.2-alpha-Windows.zip"))
            {
                MessageBox.Show("Installer cannot be initialized because you have IEIP with this program.", "IEIPInst", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
            else
            {
                InitializeComponent();
                Directory.CreateDirectory(temp);
            }
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            Directory.Delete(temp, true);
            Application.Exit();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            btnYes.Enabled = false;
            btnNo.Enabled = false;
            lblWant.Text = "";
            Complete.Text = "Status: Connecting...";
            startDownload();
        }

        private void startDownload()
        {
                WebClient client = new WebClient();
            
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri("https://github.com/JPPlaysGamer/IndustriesExes.Inc/releases/download/ieip-pre2/IEIPv0.2-alpha-Windows.zip"), temp + "\\IEIPv0.2-alpha-Windows.zip");

                

        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                double bytesIn = double.Parse(e.BytesReceived.ToString());
                double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
                double percentage = bytesIn / totalBytes * 100;
                Mbs.Text = (bytesIn / 1024 / 1024).ToString("F2") + " / " + (totalBytes / 1024/ 1024).ToString("F2") + " Mb";
                Complete.Text = "Status: Downloading...";
                IEIPProgress.Value = int.Parse(Math.Truncate(percentage).ToString());
                this.Text = "IEIP Installer - " + IEIPProgress.Value + "%";
                
            });
        }
        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
                {
                    File.Move(temp + "\\IEIPv0.2-alpha-Windows.zip",  localProg + "\\IEIPv0.2-alpha-Windows.zip");
                }
                
                Complete.Text = "Status: Completed";
                
                btnNo.Text = "Exit";
                btnNo.Enabled = true;
                
            });
        }

    }
}

This is the code from before and this is the file URL:

WebClient client = new WebClient();

client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri("https://github.com/JPPlaysGamer/IndustriesExes.Inc/releases/download/ieip-pre2/IEIPv0.2-alpha-Windows.zip"), temp + "\\IEIPv0.2-alpha-Windows.zip");

I switched Downloadfileasync to Downloadfile as the Async function has thread separated from the main and get this exception: System.Net.Webexception: The underlying connection was closed: Unexpected error in an upload. ---> System.IO.Ioexception: Unexpected EOF or 0 bytes received from transport stream.

I did everything right but now it doesn’t work anymore. Could someone explain to me about this?

1 answer

0

I discovered the problem, as I use . NET 3.5, there is no support for TLS and since I switched to version 4.5 it worked.

It may also be that Github might have used TLS connection and it didn’t work. There is a version of . NET 3.5 that supports TLS and saw that it worked. Can also work with ServicePointManager.SecurityProtocol adding Ssl3, TLS, TLS11, TLS12 and TLS13 but no . NET 3.5 does not have TLS11, TLS12 and TLS13.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.