question

sinxdesk-5609 avatar image
0 Votes"
sinxdesk-5609 asked Paul-5034 answered

How to get curl after request webclient uploadfiles

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






dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Paul-5034 avatar image
0 Votes"
Paul-5034 answered

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);
}
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.