0

I am trying to upload a file to Gitlab by using the following curl command via Package API.

curl --header "PRIVATE-TOKEN: mytokenhere" --upload-file myFile.zip https://my.gitlab.com/api/v4/projects/myProjectID/packages/generic/MyReleasePackageName/packageVersion/myFile.zip

Notes:

  • myProjectID is a numeric value something like 1200
  • packageVersion is something like 1.2.3.0

Above command works successfully, the package is uploaded to Git and I can see in GitLab in Package Registry.

Now i have translated this curl command into C# code, which is below:

class Program
{
    static async Task Main(string[] args)
    {
        await UploadMyPackageAsync();
    }

    private static async Task UploadMyPackageAsync()
    {
        try
        {
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "https://my.gitlab.com/api/v4/projects/myProjectID/packages/generic/MyReleasePackageName/packageVersion/myFile.zip");

            request.Headers.Add("PRIVATE-TOKEN", "myPrivateTokenHere");

            //request.Content = new ByteArrayContent(File.ReadAllBytes("myFile.zip"));
            request.Content = new ByteArrayContent(Properties.Resources.myFile);

            HttpResponseMessage response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Uploading package error: " + ex.ToString());
        }
    }
}

When calling client.SendAsync i get below inner exception:

The request was aborted: Could not create SSL/TLS secure channel

How can I set SSL/TLS secure channel on HttpClient? It looks like it is the problem but i do not know how to solve it. Or am i missing something in above code?

I am using standard .NET 4.5 Framework (Not .NET Core) and Visual Studio 2019.

Willy
  • 9,848
  • 22
  • 141
  • 284
  • Finally resolved by creating a HttpClientHandler and pass as a parameter to HttpClient constructor. Before doing this I also do: ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; – Willy May 04 '23 at 17:39

0 Answers0