diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-12-29 21:20:40 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-12-29 21:20:40 -0800 |
commit | 187d3c24b6a76ec0898399f738b3a4f82031ceb0 (patch) | |
tree | 5ee920acbfbf1bca76e3a7b4edfcb04e930e2367 /src/DotNetOpenAuth.Core/Reporting.cs | |
parent | 5e9014f36b2d53b8e419918675df636540ea24e2 (diff) | |
download | DotNetOpenAuth-187d3c24b6a76ec0898399f738b3a4f82031ceb0.zip DotNetOpenAuth-187d3c24b6a76ec0898399f738b3a4f82031ceb0.tar.gz DotNetOpenAuth-187d3c24b6a76ec0898399f738b3a4f82031ceb0.tar.bz2 |
Replaces IDirectWebRequestHandler with HttpClient in DNOA.Core.
Build breaks are everywhere outside of just this one project as a result.
Diffstat (limited to 'src/DotNetOpenAuth.Core/Reporting.cs')
-rw-r--r-- | src/DotNetOpenAuth.Core/Reporting.cs | 52 |
1 files changed, 14 insertions, 38 deletions
diff --git a/src/DotNetOpenAuth.Core/Reporting.cs b/src/DotNetOpenAuth.Core/Reporting.cs index f902fd6..46b3d95 100644 --- a/src/DotNetOpenAuth.Core/Reporting.cs +++ b/src/DotNetOpenAuth.Core/Reporting.cs @@ -14,10 +14,13 @@ namespace DotNetOpenAuth { using System.IO.IsolatedStorage; using System.Linq; using System.Net; + using System.Net.Http; + using System.Net.Http.Headers; using System.Reflection; using System.Security; using System.Text; using System.Threading; + using System.Threading.Tasks; using System.Web; using DotNetOpenAuth.Configuration; using DotNetOpenAuth.Messaging; @@ -73,7 +76,7 @@ namespace DotNetOpenAuth { /// <summary> /// The outgoing HTTP request handler to use for publishing reports. /// </summary> - private static IDirectWebRequestHandler webRequestHandler; + private static HttpClient webRequestHandler; /// <summary> /// A few HTTP request hosts and paths we've seen. @@ -346,7 +349,10 @@ namespace DotNetOpenAuth { file = GetIsolatedStorage(); reportOriginIdentity = GetOrCreateOriginIdentity(); - webRequestHandler = new StandardWebRequestHandler(); + var channel = new HttpClientHandler(); + channel.AllowAutoRedirect = false; + webRequestHandler = new HttpClient(channel); + webRequestHandler.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(new ProductHeaderValue(Util.LibraryVersion))); observations.Add(observedRequests = new PersistentHashSet(file, "requests.txt", 3)); observations.Add(observedCultures = new PersistentHashSet(file, "cultures.txt", 20)); observations.Add(observedFeatures = new PersistentHashSet(file, "features.txt", int.MaxValue)); @@ -426,28 +432,20 @@ namespace DotNetOpenAuth { /// Sends the usage reports to the library authors. /// </summary> /// <returns>A value indicating whether submitting the report was successful.</returns> - private static bool SendStats() { + private static async Task<bool> SendStatsAsync() { try { - var request = (HttpWebRequest)WebRequest.Create(wellKnownPostLocation); - request.UserAgent = Util.LibraryVersion; - request.AllowAutoRedirect = false; - request.Method = "POST"; - request.ContentType = "text/dnoa-report1"; Stream report = GetReport(); - request.ContentLength = report.Length; - using (var requestStream = webRequestHandler.GetRequestStream(request)) { - report.CopyTo(requestStream); - } - - using (var response = webRequestHandler.GetResponse(request)) { + var content = new StreamContent(report); + content.Headers.ContentType = new MediaTypeHeaderValue("text/dnoa-report1"); + using (var response = await webRequestHandler.PostAsync(wellKnownPostLocation, content)) { Logger.Library.Info("Statistical report submitted successfully."); // The response stream may contain a message for the webmaster. // Since as part of the report we submit the library version number, // the report receiving service may have alerts such as: // "You're using an obsolete version with exploitable security vulnerabilities." - using (var responseReader = response.GetResponseReader()) { - string line = responseReader.ReadLine(); + using (var responseReader = new StreamReader(await response.Content.ReadAsStreamAsync())) { + string line = await responseReader.ReadLineAsync(); if (line != null) { DemuxLogMessage(line); } @@ -508,28 +506,6 @@ namespace DotNetOpenAuth { } /// <summary> - /// Sends the stats report asynchronously, and careful to not throw any unhandled exceptions. - /// </summary> - [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Unhandled exceptions MUST NOT be thrown from here.")] - private static void SendStatsAsync() { - // Do it on a background thread since it could take a while and we - // don't want to slow down this request we're borrowing. - ThreadPool.QueueUserWorkItem(state => { - try { - SendStats(); - } catch (Exception ex) { - // Something bad and unexpected happened. Just deactivate to avoid more trouble. - try { - broken = true; - Logger.Library.Error("Error while trying to submit statistical report.", ex); - } catch (Exception) { - // swallow exceptions to prevent a crash. - } - } - }); - } - - /// <summary> /// Gets the isolated storage to use for reporting. /// </summary> /// <returns>An isolated storage location appropriate for our host.</returns> |