diff --git a/src/Cake.Common/Net/HttpAliases.cs b/src/Cake.Common/Net/HttpAliases.cs index 6554c069e7..77b8064e78 100644 --- a/src/Cake.Common/Net/HttpAliases.cs +++ b/src/Cake.Common/Net/HttpAliases.cs @@ -197,7 +197,7 @@ public static void DownloadFile(this ICakeContext context, Uri address, FilePath // We track the last posted value since the event seems to fire many times for the same value. var percentComplete = 0; - using (var http = new HttpClient()) + using (var http = GetHttpClient(context)) { if (!string.IsNullOrWhiteSpace(settings.Username) && !string.IsNullOrWhiteSpace(settings.Password)) { @@ -252,7 +252,7 @@ public static void UploadFile(this ICakeContext context, Uri address, FilePath f } context.Log.Verbose("Uploading file: {0}", address); - using (var client = new HttpClient()) + using (var client = GetHttpClient(context)) { client.UploadFileAsync(address, filePath.FullPath).Wait(); } @@ -309,7 +309,7 @@ public static void UploadFile(this ICakeContext context, Uri address, byte[] dat } context.Log.Verbose("Uploading file: {0}", address); - using (var client = new HttpClient()) + using (var client = GetHttpClient(context)) { client.UploadFileAsync(address, data, fileName).Wait(); } @@ -335,5 +335,18 @@ public static void UploadFile(this ICakeContext context, string address, byte[] { UploadFile(context, new Uri(address), data, fileName); } + + /// + /// Gets an pre-populated with the correct default headers such as User-Agent. + /// The returned client should be disposed of by the caller. + /// + /// The current Cake context. + /// A instance. + private static HttpClient GetHttpClient(ICakeContext context) + { + var client = new HttpClient(); + client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Cake", context.Environment.Runtime.CakeVersion.ToString())); + return client; + } } } \ No newline at end of file