Answer the question
In order to leave comments, you need to log in
How to properly upload a file to the VK server through the VK API (C#)?
Good day everyone!
Six months later, I decided to rewrite my old VK bot. In the process, I discovered that they changed the rules for uploading files to the server and simply webClient.UploadFile() no longer rolls.
"Upload the file to the upload_url obtained in the previous paragraph by making a POST request with the file field. This field must contain a file in the multipart/form-data format." - that's what they are asking now. I rarely encounter POST requests, so I don’t quite understand how to push file data there. Please help me to correctly form this same post-request.
I'm doing it this way, tell me what's wrong here, because Result is returned as an empty string!
var uri = new Uri(uploadURL);
var content = new MultipartFormDataContent();
var t = new StreamContent(File.OpenRead(path));
t.Headers.ContentType
= new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
content.Add(t, "file", "testiks.jpg");
var response = PostBuffer(uri, content);
response.Wait();
private async Task<string> PostBuffer(Uri uploadURL, MultipartFormDataContent content)
{
var baseAddress = uploadURL;
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla 228");
var response = await client.PostAsync(uploadURL, content);
return await response.Content.ReadAsStringAsync();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question