Exemple #1
0
        /// <summary>
        ///     Uploads a photo to a facebook api as an asynchronous operation.
        /// </summary>
        /// <param name="filename">
        ///     The full file path of the photo.
        /// </param>
        /// <param name="endpoint">
        ///     The target api to request.
        /// </param>
        /// <param name="queries">
        ///     The query data to append to the endpoint.
        /// </param>
        /// <returns>
        ///     The task object representing the asynchronous operation.
        /// </returns>
        private static async Task <ResponseMessage <string> > UploadAsync(string filename, Uri endpoint, params KeyValuePair <string, string>[] queries)
        {
            // Queries.
            endpoint = endpoint.AppendQueries(queries);

            // Content.
            var bytes = await File.ReadAllBytesAsync(filename);

            var photoContent = new ByteArrayContent(bytes, 0, bytes.Length);

            photoContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
            photoContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("filename", $"{Guid.NewGuid().ToString("N")}{Path.GetExtension(filename)}"));

            var content = new MultipartFormDataContent
            {
                photoContent
            };

            // Request.
            var request = new HttpRequestMessage
            {
                RequestUri = endpoint,
                Method     = HttpMethod.Post,
                Content    = content
            };

            using (var client = new HttpClient())
            {
                var response = await client.SendAsync(request);

                return(await response.ToResponseMessageAsync());
            }
        }
Exemple #2
0
 /// <summary>
 ///     Appends a query to a specified uri. If the target query already exists, it is overwritten.
 /// </summary>
 /// <param name="uri">
 ///     The target uri.
 /// </param>
 /// <param name="query">
 ///     The query to append.
 /// </param>
 /// <returns>
 ///     An <see cref="Uri"/> object.
 /// </returns>
 public static Uri AppendQuery(this Uri uri, KeyValuePair <string, string> query)
 {
     return(uri.AppendQueries(new List <KeyValuePair <string, string> >
     {
         query
     }));
 }