How to get curl after request webclient uploadfiles

sinxdesk 1 Reputation point
2021-10-17T23:06:05.843+00:00

How to get url after request using webclient upload file?

Exemple:

WebClient request = new WebClient();
request.UploadFile("https://sndup.net/", "C:\\Users\\sinxdesk\\Music\\558.mp3");

Result:

https://i.stack.imgur.com/CFBBt.png

I need to get this "https://sndup.net/9532" and put it on ClipBoard using C#

Thank you for your attention

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2021-10-17T23:59:02.07+00:00

    WebClient has been deprecated in favour of HttpClient so I've added an example using that instead:

    using System.Net;
    
    const string filepath = @"C:\Users\sinxdesk\Music\558.mp3";
    
    var filename = Path.GetFileNameWithoutExtension(filepath);
    
    using HttpClient client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false });
    
    using FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
    
    var response = await client.PostAsync("https://sndup.net/", new MultipartFormDataContent {
        { new StreamContent(stream), "file",    filename },
        { new StringContent(filename), "postname" }
    });
    
    if (response.StatusCode == HttpStatusCode.Found) {
        var url = response.Headers.GetValues("Location").First();
    
        Console.WriteLine(url);
    }
    
    0 comments No comments