private void ExecuteCommand <T>(T data, string controller, string action) { var jsonData = string.Empty; var address = string.Empty; try { var client = new WebClient(); var serializer = new JavaScriptSerializer(); jsonData = serializer.Serialize(data); client.Headers[HttpRequestHeader.ContentType] = "application/json"; address = string.Format("{0}/api/{1}/{2}", _address, controller, action); client.UploadString(address, jsonData); } catch (AggregateException exception) { throw ExpectedIssues.GetException(ExpectedIssues.FailedToExecutePost, exception.InnerException); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (TimeoutException exception) { throw ExpectedIssues.GetException(ExpectedIssues.Timeout, exception); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (WebException exception) { throw ExpectedIssues.GetException(exception); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (Exception exception) { //exception.AddData("JsonData", jsonData).AddData("Address", address); throw; } }
private void ExecuteCommand <T>(ObjectContent <T> content, string controller, string action) { try { var client = GetHttpClient(); var response = client.PostAsync(string.Format("api/{0}/{1}", controller, action), content); if (!response.Wait(_timeout)) { throw new TimeoutException("The WebAPI call exceeded the allotted time.").AddData("Timeout", _timeout.ToString()); } if (!response.Result.IsSuccessStatusCode) { throw ExpectedIssues.GetException(ExpectedIssues.ServiceCallError).AddData("StatusCode", (int)response.Result.StatusCode).AddData("StatusCodeName", response.Result.StatusCode).AddData("ReasonPhrase", response.Result.ReasonPhrase); } } catch (AggregateException exception) { throw ExpectedIssues.GetException(ExpectedIssues.FailedToExecutePost, exception.InnerException); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (TimeoutException exception) { throw ExpectedIssues.GetException(ExpectedIssues.Timeout, exception); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (WebException exception) { throw ExpectedIssues.GetException(exception); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (Exception exception) { //exception.AddData("JsonData", jsonData).AddData("Address", address); throw; } }
public static ITarget Get() { if (Configuration.Target.Instance != null) { return(Configuration.Target.Instance); } switch (Configuration.Target.Type) { case Configuration.Target.TargetType.Service: return(new ServiceTarget(Configuration.Target.Location, Configuration.Target.Timeout)); default: throw ExpectedIssues.GetException(ExpectedIssues.UnknownType).AddData("Type", Configuration.Target.Type.ToString()); } }
public string ExecuteQuery(string jsonData, string controller, string action) { var address = string.Empty; try { address = string.Format("{2}/api/{0}/{1}", controller, action, _address); var httpWebRequest = (HttpWebRequest)WebRequest.Create(address); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) streamWriter.Write(jsonData); var httpResponse = httpWebRequest.GetResponse() as HttpWebResponse; using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var responseText = streamReader.ReadToEnd(); return(responseText); } } catch (AggregateException exception) { throw ExpectedIssues.GetException(ExpectedIssues.FailedToExecutePost, exception.InnerException); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (TimeoutException exception) { throw ExpectedIssues.GetException(ExpectedIssues.Timeout, exception); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (WebException exception) { throw ExpectedIssues.GetException(exception); //.AddData("JsonData", jsonData).AddData("Address", address); } catch (Exception exception) { //exception.AddData("JsonData", jsonData).AddData("Address", address); throw; } }