public TransportResult Send(XDocument document) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_uri); request.ContentType = "application/xml"; request.Method = "POST"; var result = new TransportResult(); using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms, Encoding.GetEncoding("ISO-8859-1"))) { document.Save(sw); ms.Seek(0, SeekOrigin.Begin); var bytes = ms.ToArray(); request.ContentLength = bytes.Length + 1; Stream requestStream = null; try { requestStream = request.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); requestStream.WriteByte(0x00); } finally { if (requestStream != null) { requestStream.Close(); } } } } try { using (var response = (HttpWebResponse)request.GetResponse()) { result.Success = (response.StatusCode == HttpStatusCode.OK); using (var xr = XmlReader.Create(response.GetResponseStream())) { var content = XDocument.Load(xr); result.Content = content; } } } catch { result.Success = false; } return(result); }
public TransportResult Send(XDocument document) { var client = new TcpClient(_uri.Host, _uri.Port); using (var sendData = new MemoryStream()) { using (var xw = XmlWriter.Create(sendData, new XmlWriterSettings() { Encoding = Encoding.GetEncoding("ISO-8859-1") })) { document.Save(xw); } var networkStream = client.GetStream(); networkStream.Write(sendData.ToArray(), 0, (int)sendData.Length); networkStream.WriteByte(0); using (var receivedData = new MemoryStream()) { var buffer = new byte[256]; int read = 0; do { read = networkStream.Read(buffer, 0, buffer.Length); if (read > 0) { // Check if last byte is a zero byte, if so, don't include in receivedData if (buffer[read - 1] == 0) { read--; } receivedData.Write(buffer, 0, read); } }while (read == buffer.Length); var result = new TransportResult(); try { receivedData.Seek(0, SeekOrigin.Begin); var xr = XmlReader.Create(receivedData); result.Content = XDocument.Load(xr); result.Success = true; } catch { result.Success = false; } return(result); } } }
protected static IEnumerable <MessageResult> ParseTransportResults(IEnumerable <Message> messages, TransportResult transportResult) { return(transportResult .Content .Descendants("MSG") .Select((el) => { var id = int.Parse(el.Element("ID").Value); var message = messages.FirstOrDefault(m => m.NumInSession == id); return new MessageResult { UserReference = message.UserReference, Message = message, GatewayReference = el.Element("REF") != null ? el.Element("REF").Value : null, Status = el.Element("STATUS").Value, StatusText = el.Element("INFO") != null ? el.Element("INFO").Value : null }; })); }
public async Task <TransportResult> SendAsync(XDocument document) { var request = (HttpWebRequest)WebRequest.Create(_uri); request.ContentType = "application/xml"; request.Method = "POST"; var result = new TransportResult(); using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms, Encoding.GetEncoding("ISO-8859-1"))) { document.Save(ms); ms.Seek(0, SeekOrigin.Begin); var bytes = ms.ToArray(); request.ContentLength = bytes.Length + 1; Stream requestStream = null; try { requestStream = await request.GetRequestStreamAsync(); await requestStream.WriteAsync(bytes, 0, bytes.Length); requestStream.WriteByte(0x00); } finally { if (requestStream != null) { requestStream.Close(); } } } } try { using (var response = (HttpWebResponse)await request.GetResponseAsync()) { result.Success = (response.StatusCode == HttpStatusCode.OK); if (result.Success) { using (var responseStream = response.GetResponseStream()) using (var sr = new StreamReader(responseStream, Encoding.GetEncoding("ISO-8859-1"))) { var contentstring = await sr.ReadToEndAsync(); result.Content = XDocument.Parse(contentstring); } } } } catch { result.Success = false; } return(result); }
protected static GatewayResponse GetSendResult(IEnumerable <Message> messages, TransportResult transportResult) { var result = new GatewayResponse(); result.Results = ParseTransportResults(messages, transportResult); return(result); }