Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [SOLVED] Windows Stream Sending Garbage To Java Servlet
New Posts  All Forums:Forum Nav:

[SOLVED] Windows Stream Sending Garbage To Java Servlet

post #1 of 2
Thread Starter 
I'm trying to send some XML from a C# application to a JavaServlet. Test C# client code running on Mono (Linux side) sends the data to the Servlet with zero issues. Windows application built using the same code to send the data manages to garble it and send spurious characters which the XML processor in the Servlet chokes on.

The code is as follows: Warning: Spoiler! (Click to show)
Code:
string username = usernameInput.Text;
string password = passwordInput.Password;

XmlDocument authMessage = new XmlDocument();
XmlNode decNode = authMessage.CreateXmlDeclaration("1.0", "UTF-8", "yes");
authMessage.AppendChild(decNode);

XmlElement dataElement = authMessage.CreateElement("data");
authMessage.AppendChild(usernameElement);

XmlElement systemElement = authMessage.CreateElement("system");
systemElement.AppendChild(authMessage.CreateTextNode("TEST"));
dataElement.AppendChild(systemElement);

XmlElement commandElement = authMessage.CreateElement("command");
commandElement.AppendChild(authMessage.CreateTextNode("authenticate"));
dataElement.AppendChild(commandElement);

XmlElement usernameElement = authMessage.CreateElement("username");
usernameElement.AppendChild(authMessage.CreateTextNode(username));
dataElement.AppendChild(usernameElement);

XmlElement passwordElement = authMessage.CreateElement("password");
passwordElement.AppendChild(authMessage.CreateTextNode(password));
dataElement.AppendChild(passwordElement);

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
authMessage.Save(writer);

byte[] bytes = stream.GetBuffer();
WebRequest request = WebRequest.Create("http://192.168.1.191:8080/test/TestServlet");
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
request.Method = "POST";

Stream dataStream = request.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();

WebResponse response = request.GetResponse();

dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
            
reader.Close();
response.Close();

Has anyone run into this issue before, and if so how did you solve it? Client-side or server-side?

Many thanks. smile.gif
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
post #2 of 2
Thread Starter 
OK, so this is solved. Looks as if there is a bug in the Microsoft implementation of MemoryStream - this behaviour doesn't exist in the Mono implementation of the same class.
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [SOLVED] Windows Stream Sending Garbage To Java Servlet