How to get curl after request webclient uploadfiles

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

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.
6,918 questions
No comments
{count} votes

1 answer

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

    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);
    }
    
    No comments