Beispiel #1
0
        public static MultipartFormDataContent PayloadToMultipartFromDataContent(LineNotifyPayload payload)
        {
            MultipartFormDataContent content = new MultipartFormDataContent();

            if (payload.Message != null)
            {
                var message = new StringContent(payload.Message);
                message.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                {
                    Name = "message"
                };
                content.Add(message);
            }
            else
            {
                throw new ArgumentNullException();
            }

            if (payload.ImageFile != null)
            {
                var image = ImageToContent(payload.ImageFile);
                image.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                {
                    Name     = "imageFile",
                    FileName = "image.png"
                };
                image.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
                content.Add(image);
            }

            return(content);
        }
        public async Task <string> PostMessageAsync(LineNotifyPayload payload)
        {
            try
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Token ?? throw new ArgumentNullException());

                using (var response = await client.PostAsync(LineNotifyEndPoint, PayloadConverter.PayloadToMultipartFromDataContent(payload)))
                {
                    return(await response.Content.ReadAsStringAsync());
                }
            }
            catch (HttpRequestException re)
            {
                Console.WriteLine(re);
                return(null);
            }
            catch
            {
                return(null);
            }
        }