[Patch] Count HTTP traffic used for sync
-
14. listopadu 2011 7:54
I wanted to count the HTTP traffic used for sync so i hooked some custom code at CacheControllerBehavior.BeforeSendingRequest and CacheControllerBehavior.AfterReceivingResponse to examine the http requests.
This works well for the HttpWebResponse to retrieve its content length but there is some caveat on the windows phone platform for HttpWebRequest. Both the ContentLength property and the corresponding HTTP Header are 0 unless the request is actually send. As documented in the MSDN the header gets populated with the correct value by the silverlight framework just before request execution. So there is no chance to count traffic for client requests unless you apply a little hacking:
namespace Microsoft.Synchronization.ClientServices { class HttpCacheRequestHandler : CacheRequestHandler { ... void OnUploadGetRequestStreamCompleted(IAsyncResult asyncResult) { ... requestStream.Flush(); try { // manually write the content-length header so it can be read in the prerequest user code wrapper.WebRequest.Headers[HttpRequestHeader.ContentLength] = requestStream.Length.ToString(); } catch (ArgumentException) { // Pre-Mango OS throws: ArgumentException - The 'Content-Length' header cannot be modified directly. } requestStream.Close(); ... void OnDownloadGetRequestStreamCompleted(IAsyncResult asyncResult) { ... requestStream.Flush(); try { // manually write the content-length header so it can be read in the prerequest user code wrapper.WebRequest.Headers[HttpRequestHeader.ContentLength] = requestStream.Length.ToString(); } catch (ArgumentException) { // Pre-Mango OS throws: ArgumentException - The 'Content-Length' header cannot be modified directly. } requestStream.Close();
Senior Jack of all trades