K
K
kir_cipher2017-02-15 00:41:41
In contact with
kir_cipher, 2017-02-15 00:41:41

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();

PostBuffer method:
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

1 answer(s)
R
Rou1997, 2017-02-15
@Rou1997

Standard multipart/form-data, nothing special, you can test on your server, sketch in PHP or something else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question