I'm having issues reading in bytes sent by a Java servlet to a client written in C#.
C# Code
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://path.to/servlet");
req.Method = "POST";
req.ContentType = "text/html";
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream respStream = res.GetResponseStream();
byte[] re = new byte[512];
respStream.Read(re, 0, re.Length);
MessageBox.Show(Encoding.Default.GetString(re));
Java Code
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
ServletOutputStream resp = response.getOutputStream();
try{
resp.write(new String("Test String").getBytes());
}
catch(Exception e){
e.printStackTrace();
}
}
I realize that there are differences between Java bytes, and C# bytes (signed vs unsigned), but I have no clue how to stream java's signed bytes into something that c# can recognize. Any ideas? Do I need to tweak something on the servlet as far as how I send the information, or does the C# bit need to be modified?
The quickest thing to do is in your C# code specify the encoding, instead of
Encoding.Default.GetString(re)
You should be able to use,
System.Text.Encoding.GetEncoding(1252).GetString(re)
According to the Wikipedia entry on Windows-1252 it is a superset of ISO-8859-1 (the Java default character encoding). You might consider explicitly using UTF-8 on both sides,
System.Text.Encoding.UTF8.GetString(re)
and in Java,
new String("Test String").getBytes("UTF-8")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.