/// <summary> /// This will perform action with http client. User have to pass action name, body params and header params. /// </summary> /// <param name="actionID"></param> /// <param name="bodyParams"></param> /// <param name="headerParams"></param> public static async void PerformAction(string actionID, Dictionary <string, object> bodyParams, Dictionary <string, string> headerParams) { try { Err_Logger.Reset(); if (string.IsNullOrEmpty(actionID)) { if (Err_Logger.err_MissingAction == null) { Err_Logger.err_MissingAction = new Err_MissingAction(); } Err_Logger.err_MissingAction.MissingAction = string.Format(ValidatorHandler.ERR_EMPTY_ACTION_NAME, "Action Name"); //await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => //{ // MessageDialog message = new MessageDialog(string.Format(ValidatorHandler.ERR_EMPTY_ACTION_NAME, "Action Name")); // await message.ShowAsync(); // return; //}); } ValidatorHandler oValidatorHandler = new ValidatorHandler(); oValidatorHandler.checkHeaders(actionID, headerParams); string strResult = await oValidatorHandler.checkBodyParameters(actionID, bodyParams); JObject jsonObject = new JObject(); jsonObject["Err_BodyParameter"] = JObject.Parse(JsonConvert.SerializeObject(Err_Logger.err_BodyParameter)); jsonObject["Err_HeaderParameter"] = JObject.Parse(JsonConvert.SerializeObject(Err_Logger.err_HeaderParameter)); jsonObject["MissingAction"] = Err_Logger.err_MissingAction.MissingAction; strResult = jsonObject.ToString(); if (!string.IsNullOrEmpty(strResult)) { MessageDialog message = new MessageDialog(strResult); await message.ShowAsync(); } else { API oAPI = new API(); oAPI.ActionID = actionID; var result = await oAPI.GetAPIDetail(); if (result != null) { RequestType m_RequestType = RequestType.POST; switch (result.request_type) { case "POST": m_RequestType = RequestType.POST; break; case "GET": m_RequestType = RequestType.GET; break; case "PUT": m_RequestType = RequestType.PUT; break; case "DELETE": m_RequestType = RequestType.DELETE; break; default: break; } Dictionary <string, string> tempBodyParams = bodyParams.ToDictionary(k => k.Key, k => k.Value.ToString()); StatusAndResponseClass m_StatusAndResponseClass = await Request(m_RequestType, actionID, tempBodyParams, headerParams); } } } catch (Exception ex) { } }
/// <summary> /// Common method to request in POST, GET, PUT and DELETE form to get data in json format. /// </summary> /// <param name="requestType"></param> /// <param name="url"></param> /// <param name="data"></param> /// <returns name="StatusAndResponseClass"></returns> public static async Task <StatusAndResponseClass> Request(RequestType requestType, string url, string data, Dictionary <string, string> header) { StatusAndResponseClass getResponse = new StatusAndResponseClass(); HttpClient client = new HttpClient(); if (header != null && header.Count() > 0) { foreach (var item in header) { client.DefaultRequestHeaders.Add(item.Key, item.Value); } } HttpResponseMessage response = new HttpResponseMessage(); StringContent queryString; System.Diagnostics.Debug.WriteLine("PostStringJsonData (URL): " + url); System.Diagnostics.Debug.WriteLine("PostStringJsonData (POSTDATA): " + data); try { switch (requestType) { case RequestType.POST: queryString = new StringContent(data, UTF8Encoding.UTF8, "application/json"); response = await client.PostAsync(new Uri(url), queryString); break; case RequestType.GET: response = await client.GetAsync(new Uri(url)); break; case RequestType.PUT: queryString = new StringContent(data, UTF8Encoding.UTF8, "application/json"); response = await client.PutAsync(new Uri(url), queryString); break; case RequestType.DELETE: response = await client.DeleteAsync(new Uri(url + data)); break; default: break; } client.Dispose(); HttpStatusCode statuscode = response.StatusCode; string responseBody = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { getResponse.responseString = responseBody; } else { getResponse.responseString = responseBody; } getResponse.statusCode = Convert.ToInt32(statuscode); if (getResponse.statusCode == 404) { getResponse.responseString = string.Empty; } return(getResponse); } catch (Exception ex) { return(getResponse); } }
/// <summary> /// Common method to request in POST, GET, PUT and DELETE form to get data in key value format. /// </summary> /// <param name="requestType"></param> /// <param name="url"></param> /// <param name="data"></param> /// <returns name="StatusAndResponseClass"></returns> public static async Task <StatusAndResponseClass> Request(RequestType requestType, string url, Dictionary <string, string> data, Dictionary <string, string> header) { StatusAndResponseClass getResponse = new StatusAndResponseClass(); HttpClient client = new HttpClient(); if (header != null && header.Count() > 0) { foreach (var item in header) { client.DefaultRequestHeaders.Add(item.Key, item.Value); } } IProgress <Windows.Web.Http.HttpProgress> progress = new Progress <Windows.Web.Http.HttpProgress>(ProgressHandler); HttpResponseMessage response = new HttpResponseMessage(); HttpContent content; System.Diagnostics.Debug.WriteLine("PostStringJsonData (URL): " + url); System.Diagnostics.Debug.WriteLine("PostStringJsonData (POSTDATA): " + data); try { switch (requestType) { case RequestType.POST: content = new FormUrlEncodedContent(data); response = await client.PostAsync(new Uri(url), content, cancellationToken.Token); break; case RequestType.GET: var message = new HttpRequestMessage(HttpMethod.Get, url); response = await client.SendAsync(message, cancellationToken.Token); //response = await client.GetAsync(new Uri(url)); break; case RequestType.PUT: content = new FormUrlEncodedContent(data); response = await client.PutAsync(new Uri(url), content, cancellationToken.Token); break; case RequestType.DELETE: content = new FormUrlEncodedContent(data); response = await client.DeleteAsync(new Uri(url + content), cancellationToken.Token); break; default: break; } client.Dispose(); HttpStatusCode statuscode = response.StatusCode; string responseBody = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { getResponse.responseString = responseBody; } else { getResponse.responseString = responseBody; } getResponse.statusCode = Convert.ToInt32(statuscode); if (getResponse.statusCode == 404) { getResponse.responseString = string.Empty; } return(getResponse); } catch (Exception ex) { return(getResponse); } }