Created
June 17, 2022 14:21
-
-
Save geiltonxavier/f49b6ef15f7fe9a4ef9b4dc71162d3d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Upload(); | |
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests | |
// https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ | |
void Upload() | |
{ | |
Console.WriteLine("init - upload file"); | |
using (var client = new HttpClient()) | |
{ | |
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "API Service"); | |
client.DefaultRequestHeaders.TryAddWithoutValidation("accept", "*/*"); | |
using (var content = new MultipartFormDataContent()) | |
{ | |
var path = @"you_file_path"; | |
string fileName = Path.GetFileName(path); | |
Console.WriteLine($"upload file name {fileName}"); | |
FileStream fs = File.OpenRead(path); | |
var streamContent = new StreamContent(fs); | |
content.Add(streamContent, "file", Path.GetFileName(path)); | |
Console.WriteLine($"Post file {fileName} Async"); | |
var url = "service_upload_url"; | |
Task<HttpResponseMessage> message = client.PostAsync(url, content); | |
Console.WriteLine($" PostAsync"); | |
var input = message.Result.Content.ReadAsStringAsync(); | |
Console.WriteLine($"Result: {input.Result}"); | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment