protected override void Apply(HttpRequest request) { base.Apply(request); request.Headers.ContentType = JsonRpcContentType; var parameterData = new object[JsonParameters.Count]; var parameterSummary = new string[JsonParameters.Count]; for (var i = 0; i < JsonParameters.Count; i++) { ConvertParameter(JsonParameters[i], out parameterData[i], out parameterSummary[i]); } var message = new Dictionary <string, object>(); message["jsonrpc"] = "2.0"; message["method"] = JsonMethod; message["params"] = parameterData; message["id"] = CreateNextId(); request.SetContent(message.ToJson()); if (request.ContentSummary == null) { request.ContentSummary = string.Format("{0}({1})", JsonMethod, string.Join(", ", parameterSummary)); } }
protected override void Apply(HttpRequest request) { base.Apply(request); request.Headers.ContentType = XmlRpcContentType; var methodCallElements = new List <XElement> { new XElement("methodName", XmlMethod) }; if (XmlParameters.Any()) { var argElements = XmlParameters.Select(x => new XElement("param", ConvertParameter(x))).ToList(); var paramsElement = new XElement("params", argElements); methodCallElements.Add(paramsElement); } var message = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("methodCall", methodCallElements)); var body = message.ToString(); Logger.Debug($"Executing remote method: {XmlMethod}"); Logger.Trace($"methodCall {XmlMethod} body:\n{body}"); request.SetContent(body); }
protected virtual void ApplyFormData(HttpRequest request) { if (FormData.Empty()) { return; } if (request.ContentData != null) { throw new ApplicationException("Cannot send HttpRequest Body and FormData simultaneously."); } var shouldSendAsMultipart = FormData.Any(v => v.ContentType != null || v.FileName != null || v.ContentData.Length > 1024); if (shouldSendAsMultipart) { var boundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x14"); var partBoundary = string.Format("--{0}\r\n", boundary); var endBoundary = string.Format("--{0}--\r\n", boundary); var bodyStream = new MemoryStream(); var summary = new StringBuilder(); using (var writer = new StreamWriter(bodyStream, new UTF8Encoding(false))) { foreach (var formData in FormData) { writer.Write(partBoundary); writer.Write("Content-Disposition: form-data"); if (formData.Name.IsNotNullOrWhiteSpace()) { writer.Write("; name=\"{0}\"", formData.Name); } if (formData.FileName.IsNotNullOrWhiteSpace()) { writer.Write("; filename=\"{0}\"", formData.FileName); } writer.Write("\r\n"); if (formData.ContentType.IsNotNullOrWhiteSpace()) { writer.Write("Content-Type: {0}\r\n", formData.ContentType); } writer.Write("\r\n"); writer.Flush(); bodyStream.Write(formData.ContentData, 0, formData.ContentData.Length); writer.Write("\r\n"); if (formData.FileName.IsNotNullOrWhiteSpace()) { summary.AppendFormat("\r\n{0}={1} ({2} bytes)", formData.Name, formData.FileName, formData.ContentData.Length); } else { summary.AppendFormat("\r\n{0}={1}", formData.Name, Encoding.UTF8.GetString(formData.ContentData)); } } writer.Write(endBoundary); } var body = bodyStream.ToArray(); // TODO: Scan through body to see if we have a boundary collision? request.Headers.ContentType = "multipart/form-data; boundary=" + boundary; request.SetContent(body); if (request.ContentSummary == null) { request.ContentSummary = summary.ToString(); } } else { var parameters = FormData.Select(v => string.Format("{0}={1}", v.Name, Uri.EscapeDataString(Encoding.UTF8.GetString(v.ContentData)))); var urlencoded = string.Join("&", parameters); var body = Encoding.UTF8.GetBytes(urlencoded); request.Headers.ContentType = "application/x-www-form-urlencoded"; request.SetContent(body); if (request.ContentSummary == null) { request.ContentSummary = urlencoded; } } }
protected virtual void ApplyFormData(HttpRequest request) { if (FormData.Empty()) return; if (request.ContentData != null) { throw new ApplicationException("Cannot send HttpRequest Body and FormData simultaneously."); } var shouldSendAsMultipart = FormData.Any(v => v.ContentType != null || v.FileName != null || v.ContentData.Length > 1024); if (shouldSendAsMultipart) { var boundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x14"); var partBoundary = string.Format("--{0}\r\n", boundary); var endBoundary = string.Format("--{0}--\r\n", boundary); var bodyStream = new MemoryStream(); var summary = new StringBuilder(); using (var writer = new StreamWriter(bodyStream, new UTF8Encoding(false))) { foreach (var formData in FormData) { writer.Write(partBoundary); writer.Write("Content-Disposition: form-data"); if (formData.Name.IsNotNullOrWhiteSpace()) writer.Write("; name=\"{0}\"", formData.Name); if (formData.FileName.IsNotNullOrWhiteSpace()) writer.Write("; filename=\"{0}\"", formData.FileName); writer.Write("\r\n"); if (formData.ContentType.IsNotNullOrWhiteSpace()) writer.Write("Content-Type: {0}\r\n", formData.ContentType); writer.Write("\r\n"); writer.Flush(); bodyStream.Write(formData.ContentData, 0, formData.ContentData.Length); writer.Write("\r\n"); if (formData.FileName.IsNotNullOrWhiteSpace()) { summary.AppendFormat("\r\n{0}={1} ({2} bytes)", formData.Name, formData.FileName, formData.ContentData.Length); } else { summary.AppendFormat("\r\n{0}={1}", formData.Name, Encoding.UTF8.GetString(formData.ContentData)); } } writer.Write(endBoundary); } var body = bodyStream.ToArray(); // TODO: Scan through body to see if we have a boundary collision? request.Headers.ContentType = "multipart/form-data; boundary=" + boundary; request.SetContent(body); if (request.ContentSummary == null) { request.ContentSummary = summary.ToString(); } } else { var parameters = FormData.Select(v => string.Format("{0}={1}", v.Name, Uri.EscapeDataString(Encoding.UTF8.GetString(v.ContentData)))); var urlencoded = string.Join("&", parameters); var body = Encoding.UTF8.GetBytes(urlencoded); request.Headers.ContentType = "application/x-www-form-urlencoded"; request.SetContent(body); if (request.ContentSummary == null) { request.ContentSummary = urlencoded; } } }
protected override void Apply(HttpRequest request) { base.Apply(request); request.Headers.ContentType = JsonRpcContentType; var parameterData = new object[JsonParameters.Count]; var parameterSummary = new string[JsonParameters.Count]; for (var i = 0; i < JsonParameters.Count; i++) { ConvertParameter(JsonParameters[i], out parameterData[i], out parameterSummary[i]); } var message = new Dictionary<string, object>(); message["jsonrpc"] = "2.0"; message["method"] = JsonMethod; message["params"] = parameterData; message["id"] = CreateNextId(); request.SetContent(message.ToJson()); if (request.ContentSummary == null) { request.ContentSummary = string.Format("{0}({1})", JsonMethod, string.Join(", ", parameterSummary)); } }