Exemple #1
0
        public async Task <VkUploadPhotoResponse> UploadPhoto(string url, string fileName, Stream photoStream)
        {
            var client = new HttpClient();

            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            var    content  = new MultipartFormDataContent(boundary);

            var fileContent = new StreamContent(photoStream);

            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = fileName,
                Name     = "photo"
            };

            content.Add(fileContent);

            var responseMessage = await client.PostAsync(new Uri(url), content);

            byte[] bytes = await responseMessage.Content.ReadAsByteArrayAsync();

            Encoding encoding = Encoding.UTF8; //VK returns windows-1251 which causes exception on Win10
            string   response = encoding.GetString(bytes, 0, bytes.Length);
            var      json     = JObject.Parse(response);

            return(VkUploadPhotoResponse.FromJson(json));
        }
Exemple #2
0
        public async Task <VkUploadPhotoResponse> UploadWallPhoto(string url, string fileName, Stream photoStream)
        {
            var client = new HttpClient();

            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            var    content  = new MultipartFormDataContent(boundary);

            var fileContent = new StreamContent(photoStream);

            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = fileName,
                Name     = "photo"
            };

            content.Add(fileContent);

            var responseMessage = await client.PostAsync(new Uri(url), content);

            var response = await responseMessage.Content.ReadAsStringAsync();

            var json = JObject.Parse(response);

            return(VkUploadPhotoResponse.FromJson(json));
        }
        public static VkUploadPhotoResponse FromJson(JObject json)
        {
            if (json == null)
                throw new ArgumentNullException("json");

            var result = new VkUploadPhotoResponse();
            result.Server = (string) json["server"];
            result.Photo = (string) json["photo"];
            result.Hash = (string) json["hash"];

            return result;
        }
Exemple #4
0
        public static VkUploadPhotoResponse FromJson(JObject json)
        {
            if (json == null)
            {
                throw new ArgumentNullException("json");
            }

            var result = new VkUploadPhotoResponse();

            result.Server = (string)json["server"];
            result.Photo  = (string)json["photo"];
            result.Hash   = (string)json["hash"];

            return(result);
        }