/// <summary> /// Issues the BeginGetResponse call for the HttpWebRequest /// </summary> /// <param name="wrapper">AsyncArgsWrapper object</param> private void GetWebResponse(AsyncArgsWrapper wrapper) { var requestData = new TimeoutRequestData() { Request = wrapper.WebRequest }; // Send the request and wait for the response. if (wrapper.CacheRequest.RequestType == CacheRequestType.UploadChanges) { lock (_timeoutSync) { IAsyncResult resultUpload = wrapper.WebRequest.BeginGetResponse(OnUploadGetResponseCompleted, wrapper); requestData.Handle = ThreadPool.RegisterWaitForSingleObject(resultUpload.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), requestData, TIMEOUT, true); } } else { lock (_timeoutSync) { IAsyncResult resultDownload = wrapper.WebRequest.BeginGetResponse(OnDownloadGetResponseCompleted, wrapper); requestData.Handle = ThreadPool.RegisterWaitForSingleObject(resultDownload.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), requestData, TIMEOUT, true); } } }
/// <summary> /// Method that does the actual processing. /// 1. It first creates an HttpWebRequest /// 2. Fills in the required method type and parameters. /// 3. Attaches the user specified ICredentials. /// 4. Serializes the input params (Server blob for downloads and input feed for uploads) /// 5. If user has specified an BeforeSendingRequest callback then invokes it /// 6. Else proceeds to issue the request /// </summary> /// <param name="wrapper">AsyncArgsWrapper object</param> void ProcessRequest(AsyncArgsWrapper wrapper) { try { StringBuilder requestUri = new StringBuilder(); requestUri.AppendFormat("{0}{1}{2}/{3}", base.BaseUri, (base.BaseUri.ToString().EndsWith("/")) ? string.Empty : "/", Uri.EscapeUriString(base.ScopeName), wrapper.CacheRequest.RequestType.ToString()); string prefix = "?"; // Add the scope params if any foreach (KeyValuePair <string, string> kvp in this._scopeParameters) { requestUri.AppendFormat("{0}{1}={2}", prefix, Uri.EscapeUriString(kvp.Key), Uri.EscapeUriString(kvp.Value)); if (prefix.Equals("?")) { prefix = "&"; } } //requestUri.AppendFormat("{0}{1}={2}", prefix, Uri.EscapeUriString("user"), Uri.EscapeUriString("test")); //requestUri.AppendFormat("{0}{1}={2}", "&", Uri.EscapeUriString("pwd"), Uri.EscapeUriString("test")); // CreateInstance the WebRequest HttpWebRequest webRequest = null; if (this._credentials != null) { // CreateInstance the Client Http request //webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.CreateInstance(new Uri(requestUri.ToString())); webRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(requestUri.ToString().ToCurrentScheme(ApplicationContext.Current.Settings.HttpsDisabled))); NetworkCredential credential = this._credentials.GetCredential(BaseUri, "Basic"); // Add credentials webRequest.Credentials = this._credentials; string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(credential.UserName + ":" + credential.Password)); webRequest.Headers.Add("Authorization", "Basic " + svcCredentials); webRequest.Headers.Add("configname:" + behaviors.ConfigName); webRequest.Headers.Add("configversion:" + behaviors.ConfigVersion); webRequest.Headers.Add("coreversion", behaviors.CoreVersion.ToString()); } else { // Use WebRequest.CreateInstance the request. This uses any user defined prefix preferences for certain paths webRequest = (HttpWebRequest)WebRequest.Create(requestUri.ToString().ToCurrentScheme(ApplicationContext.Current.Settings.HttpsDisabled)); } foreach (var item in behaviors.DeviceInfo) { webRequest.Headers.Add(item.Key, item.Value); } webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; webRequest.Timeout = 5000; // Set the method type //webRequest.Timeout = 10; //webRequest.ReadWriteTimeout = 10; webRequest.Method = "POST"; webRequest.Accept = ApplicationContext.Current.Settings.BitMobileFormatterDisabled ? "application/atom+xml" : "application/bitmobile"; webRequest.ContentType = ApplicationContext.Current.Settings.BitMobileFormatterDisabled ? "application/atom+xml" : "application/bitmobile"; wrapper.WebRequest = webRequest; var requestData = new TimeoutRequestData() { Request = wrapper.WebRequest }; // Get the request stream if (wrapper.CacheRequest.RequestType == CacheRequestType.UploadChanges) { lock (_timeoutSync) { IAsyncResult resultUpload = webRequest.BeginGetRequestStream(OnUploadGetRequestStreamCompleted, wrapper); requestData.Handle = ThreadPool.RegisterWaitForSingleObject(resultUpload.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), requestData, TIMEOUT, true); } } else { lock (_timeoutSync) { IAsyncResult resultDownload = webRequest.BeginGetRequestStream(OnDownloadGetRequestStreamCompleted, wrapper); requestData.Handle = ThreadPool.RegisterWaitForSingleObject(resultDownload.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), requestData, TIMEOUT, true); } } } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } wrapper.Error = e; this._workerManager.CompleteWorkRequest(wrapper.WorkerRequest, wrapper); } }
/// <summary> /// Method that does the actual processing. /// 1. It first creates an HttpWebRequest /// 2. Fills in the required method type and parameters. /// 3. Attaches the user specified ICredentials. /// 4. Serializes the input params (Server blob for downloads and input feed for uploads) /// 5. If user has specified an BeforeSendingRequest callback then invokes it /// 6. Else proceeds to issue the request /// </summary> /// <param name="wrapper">AsyncArgsWrapper object</param> void ProcessRequest(AsyncArgsWrapper wrapper) { try { StringBuilder requestUri = new StringBuilder(); requestUri.AppendFormat("{0}{1}{2}/{3}", base.BaseUri, (base.BaseUri.ToString().EndsWith("/")) ? string.Empty : "/", Uri.EscapeUriString(base.ScopeName), wrapper.CacheRequest.RequestType.ToString()); string prefix = "?"; // Add the scope params if any foreach (KeyValuePair<string, string> kvp in this._scopeParameters) { requestUri.AppendFormat("{0}{1}={2}", prefix, Uri.EscapeUriString(kvp.Key), Uri.EscapeUriString(kvp.Value)); if (prefix.Equals("?")) { prefix = "&"; } } //requestUri.AppendFormat("{0}{1}={2}", prefix, Uri.EscapeUriString("user"), Uri.EscapeUriString("test")); //requestUri.AppendFormat("{0}{1}={2}", "&", Uri.EscapeUriString("pwd"), Uri.EscapeUriString("test")); // CreateInstance the WebRequest HttpWebRequest webRequest = null; if (this._credentials != null) { // CreateInstance the Client Http request //webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.CreateInstance(new Uri(requestUri.ToString())); webRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(requestUri.ToString().ToCurrentScheme(ApplicationContext.Current.Settings.HttpsDisabled))); NetworkCredential credential = this._credentials.GetCredential(BaseUri, "Basic"); // Add credentials webRequest.Credentials = this._credentials; string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(credential.UserName + ":" + credential.Password)); webRequest.Headers.Add("Authorization", "Basic " + svcCredentials); webRequest.Headers.Add("configname:" + behaviors.ConfigName); webRequest.Headers.Add("configversion:" + behaviors.ConfigVersion); webRequest.Headers.Add("coreversion", behaviors.CoreVersion.ToString()); } else { // Use WebRequest.CreateInstance the request. This uses any user defined prefix preferences for certain paths webRequest = (HttpWebRequest)WebRequest.Create(requestUri.ToString().ToCurrentScheme(ApplicationContext.Current.Settings.HttpsDisabled)); } foreach (var item in behaviors.DeviceInfo) webRequest.Headers.Add(item.Key, item.Value); webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; webRequest.Timeout = 5000; // Set the method type //webRequest.Timeout = 10; //webRequest.ReadWriteTimeout = 10; webRequest.Method = "POST"; webRequest.Accept = ApplicationContext.Current.Settings.BitMobileFormatterDisabled ? "application/atom+xml" : "application/bitmobile"; webRequest.ContentType = ApplicationContext.Current.Settings.BitMobileFormatterDisabled ? "application/atom+xml" : "application/bitmobile"; wrapper.WebRequest = webRequest; var requestData = new TimeoutRequestData() { Request = wrapper.WebRequest }; // Get the request stream if (wrapper.CacheRequest.RequestType == CacheRequestType.UploadChanges) { lock (_timeoutSync) { IAsyncResult resultUpload = webRequest.BeginGetRequestStream(OnUploadGetRequestStreamCompleted, wrapper); requestData.Handle = ThreadPool.RegisterWaitForSingleObject(resultUpload.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), requestData, TIMEOUT, true); } } else { lock (_timeoutSync) { IAsyncResult resultDownload = webRequest.BeginGetRequestStream(OnDownloadGetRequestStreamCompleted, wrapper); requestData.Handle = ThreadPool.RegisterWaitForSingleObject(resultDownload.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), requestData, TIMEOUT, true); } } } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } wrapper.Error = e; this._workerManager.CompleteWorkRequest(wrapper.WorkerRequest, wrapper); } }