/// <summary> /// Consumes the service at the given URL by invoking a HTTP POST. /// </summary> /// <param name="url">The service endpoint URL.</param> /// <param name="json">The JSON string.</param> /// <returns>The service method response.</returns> public string Post(string url, string json) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.AllowWriteStreamBuffering = true; request.KeepAlive = false; request.Credentials = CredentialCache.DefaultCredentials; request.ContentType = "application/json"; //request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; //string parameters = string.Format("{0}={1}", Uri.EscapeDataString("value"), Uri.EscapeDataString(json)); string parameters = json; Stream requestStream = request.GetRequestStream(); StreamWriter writer = new StreamWriter(requestStream); writer.Write(parameters); writer.Close(); // Execute the request. string result = HttpRequestAgent.ExecuteRequest(request); return(result); }
/// <summary> /// Consumes the service at the given URL by invoking a HTTP GET. /// </summary> /// <param name="url">The service endpoint URL.</param> /// <returns>The service method response.</returns> public string Getc(string url) { // Used to build entire response StringBuilder builder = new StringBuilder(); // Used on each read operation byte[] buffer = new byte[8192]; // Prepare the web service we will consume. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // Execute the request. string result = HttpRequestAgent.ExecuteRequest(request); return(result); }
public JsonResult CalculateBrutoNetto(dynamic parameters) { ServiceResponse response = default(ServiceResponse); using (Session session = ApplicationModel.Current.CreateSession(new SecurityToken(this.HttpContext.Request.Url.Host, this.HttpContext.User.Identity.Name))) { try { HttpRequestAgent connector = new HttpRequestAgent(); string value = connector.Post("https://brutnet.attentia.be/GetBrutoNettoBerekening?onlyValidate=true", parameters); //string value = "{ \"Bruto\": \"2500.0\", \"Netto\": \"1250.00\" }"; if (string.IsNullOrEmpty(value)) HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound; ResponseStatus status = string.IsNullOrEmpty(value) ? ResponseStatus.NO_DATA : ResponseStatus.OK; response = new ServiceDataResponse(RestVersion0100Controller.API_VERSION, status, value); } catch (Exception ex) { HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; response = new ServiceErrorResponse(RestVersion0100Controller.API_VERSION, ResponseStatus.ERROR, new ServiceError(ex)); } } return this.Json(response); }