StringContent CreateMessageEntityBody(Message message) { var messageToSend = new RestMSMessage { Address = message.Header.Topic, MessageId = message.Header.Id.ToString(), Content = new RestMSMessageContent { Value = message.Body.Value, Type = MediaTypeNames.Text.Plain, Encoding = Encoding.ASCII.WebName } }; var messageHeaders = new List <RestMSMessageHeader> { new RestMSMessageHeader { Name = "MessageType", Value = message.Header.MessageType.ToString() } }; messageHeaders.AddRange(message.Header.Bag.Select(customHeader => new RestMSMessageHeader { Name = customHeader.Key, Value = Encoding.ASCII.GetString((byte[])customHeader.Value) })); messageToSend.Headers = messageHeaders.ToArray(); string messageContent; if (!XmlRequestBuilder.TryBuild(messageToSend, out messageContent)) { return(null); } return(new StringContent(messageContent)); }
/// <summary> /// Creates the entity body. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="feed">The feed.</param> /// <returns>StringContent.</returns> public StringContent CreateEntityBody <T>(T feed) where T : class, new() { string feedRequest; if (!XmlRequestBuilder.TryBuild(feed, out feedRequest)) { return(null); } return(new StringContent(feedRequest)); }
public string Run() { var client = new HttpClientGateway().Client(); try { client.BaseAddress = _baseAddres; var order = new AddOrderModel() { CustomerName = "Winnie the Pooh", Description = "Pot of Honey", DueDate = DateTime.UtcNow.ToString("o") }; string orderBody; XmlRequestBuilder.TryBuild(order, out orderBody); var requestMessage = CreateRequest("orders", new StringContent(orderBody)); var response = client.SendAsync(requestMessage).Result; response.EnsureSuccessStatusCode(); return(response.Content.ReadAsStringAsync().Result); } catch (AggregateException ae) { foreach (var e in ae.Flatten().InnerExceptions) { Console.Write(e.Message); if (e.InnerException != null) { Console.WriteLine(" : " + e.InnerException); } else { Console.WriteLine(); } } } catch (Exception he) { Console.WriteLine("Exception talking to server: {0}", he); } finally { client.Dispose(); } return(null); }
private string PostOrder(HttpClient client) { var order = new AddOrderModel() { CustomerName = "Winnie the Pooh", Description = "Pot of Honey", DueDate = DateTime.UtcNow.ToString("o") }; string orderBody; XmlRequestBuilder.TryBuild(order, out orderBody); var requestMessage = CreateRequest("orders", new StringContent(orderBody)); var response = client.SendAsync(requestMessage).Result; response.EnsureSuccessStatusCode(); return(response.Content.ReadAsStringAsync().Result); }