How can you emulate object serialization on java in C#
I need to call a servlet call for an automation of a java applet using c#. What the java applet is it calls a servlet using a URL Connection object.
URL servlet = new URL(servletProtocol, servletHost, servletPort, "/" + ServletName);
URLConnection con = servlet.openConnection();
con.setDoOutput(true);
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
// Write several parameters strings
out.writeObject(param[0]);
out.writeObject(param[1]);
out.flush();
out.close();
The problem is i need to simulate this using c#. I believe the counterpart object would be HttpWebRequest
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(servletPath);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(param[0],0,param[0].length);
newStream.Write(param[1],0,param[1].length);
newStream.Close();
How do I write the string as a serialized java string? Is there any workaround here? According to the documentation of ObjectOutputStream in java, it serialize the object except for primitive type. I know String is class, so does it serialzie it like an object or some special case?
I have tried one solution, I have imported the IKVM (http://www.ikvm.net/) java virtual machine in my reference and am trying to use the java.io library in Java. Unforunately, when the ObjectInputStream constructor is called, a "invalid stream header" is thrown.
Here is my altered code:
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
List<byte> lByte = new List<byte>();
using (StreamReader sr = new StreamReader(myRequest.GetResponse().GetResponseStream()))
{
while (sr.Peek() >= 0)
{
lByte.Add((byte)sr.Read());
}
}
byte[] bArr = lByte.ToArray();
ObjectInputStream inputStream = null;
try
{
//Construct the ObjectInputStream object
开发者_高级运维inputStream = new ObjectInputStream(new ByteArrayInputStream(bArr));
Object obj = null;
while ((obj = inputStream.readObject()) != null)
{
string objStr = obj as string;
}
}
catch (java.lang.Exception ex)
I finally got to get this to work.
The problem is on reading the data in .NET instead of using StreamReader, I need to use the Stream object immediately. Anyway just leaving it here in case it helps others with their problem:
Wrong Code:
List<byte> lByte = new List<byte>();
using (StreamReader sr = new StreamReader(myRequest.GetResponse().GetResponseStream()))
{
while (sr.Peek() >= 0)
{
lByte.Add((byte)sr.Read());
}
}
Correct Code:
byte[] buffer = new byte[1000];
using (Stream sr = myRequest.GetResponse().GetResponseStream())
{
sr.Read(buffer, 0, 1000);
}
If you have control over the serialization/deserialization on the Java side, your best bet is to use a cross-platform serialization protocol such as Protocol Buffers. For C++, Java, and Python:
http://code.google.com/p/protobuf/
For .NET, Jon Skeet wrote a port:
http://code.google.com/p/protobuf-net/
精彩评论