/// <summary> /// Wraps WebClient request in single method. /// </summary> /// <param name="pageMethod"></param> /// <param name="method"></param> /// <param name="requestData"></param> /// <returns>Json</returns> private dynamic PerformRequest(string pageMethod, HttpMethod method, NameValueCollection requestData) { using (var client = new EnhancedWebClient()) { if (this.Credentials == null) { client.UseDefaultCredentials = true; } else { // Force sending credentials on first request if (this.ForceSendAuthorization) { var basicAuth = this.Credentials.GetCredential(this.BaseUri, "Basic"); if (basicAuth != null) { var cre = string.Format("{0}:{1}", basicAuth.UserName, basicAuth.Password); var base64 = Convert.ToBase64String(Encoding.GetBytes(cre)); client.Headers.Add("Authorization", "Basic " + base64); } } client.Credentials = this.Credentials; } // Send ACCEPT to accept JSON by default client.Headers.Add(HttpRequestHeader.Accept, "application/json"); if (method == HttpMethod.POST) { if (requestData == null) { throw new ArgumentNullException("requestData", "For POST, form data cannot be null."); } client.Headers.Add("Content-type", "application/x-www-form-urlencoded"); return(Json.Parse(Encoding.GetString(client.UploadValues(pageMethod.ToRelativeUri(BaseUri), requestData)))); } else { client.QueryString = requestData; return(Json.Parse(client.DownloadString(pageMethod.ToRelativeUri(BaseUri)))); } } }
/// <summary> /// Wraps WebClient request in single method. /// </summary> /// <param name="pageMethod"></param> /// <param name="method"></param> /// <param name="requestData"></param> /// <returns>Json</returns> private dynamic PerformRequest(string pageMethod, HttpMethod method, NameValueCollection requestData) { using (var client = new EnhancedWebClient()) { if (this.Credentials == null) client.UseDefaultCredentials = true; else { // Force sending credentials on first request if (this.ForceSendAuthorization) { var basicAuth = this.Credentials.GetCredential(this.BaseUri, "Basic"); if (basicAuth != null) { var cre = string.Format("{0}:{1}", basicAuth.UserName, basicAuth.Password); var base64 = Convert.ToBase64String(Encoding.GetBytes(cre)); client.Headers.Add("Authorization", "Basic " + base64); } } client.Credentials = this.Credentials; } // Send ACCEPT to accept JSON by default client.Headers.Add(HttpRequestHeader.Accept, "application/json"); if (method == HttpMethod.POST) { if (requestData == null) throw new ArgumentNullException("requestData", "For POST, form data cannot be null."); client.Headers.Add("Content-type", "application/x-www-form-urlencoded"); return Json.Parse(Encoding.GetString(client.UploadValues(pageMethod.ToRelativeUri(BaseUri), requestData))); } else { client.QueryString = requestData; return Json.Parse(client.DownloadString(pageMethod.ToRelativeUri(BaseUri))); } } }