如何在 C# 中模拟 java 上的对象序列化
我需要使用 C# 调用 Servlet 来实现 Java 小程序的自动化。 java applet 是什么,它使用 URL Connection 对象调用 servlet。
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();
问题是我需要使用 C# 来模拟它。我相信对应的对象将是 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();
如何将字符串写为序列化的 java 字符串?这里有什么解决方法吗?根据java中ObjectOutputStream的文档,它序列化除原始类型之外的对象。我知道 String 是类,那么它是否像对象或某种特殊情况一样序列化它?
我尝试了一种解决方案,我在参考文献中导入了 IKVM (http://www.ikvm.net/) java 虚拟机,并尝试在 Java 中使用 java.io 库。不幸的是,当调用 ObjectInputStream 构造函数时,会抛出“无效的流标头”。
这是我修改后的代码:
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 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于让它发挥作用了。
问题在于读取.NET 中的数据而不是使用 StreamReader,我需要立即使用 Stream 对象。无论如何,把它留在这里,以防它帮助其他人解决他们的问题:
错误的代码:
正确的代码:
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:
Correct Code:
如果您可以控制 Java 端的序列化/反序列化,那么最好的选择是使用跨平台序列化协议,例如 Protocol Buffers。对于 C++、Java 和 Python:
http://code.google.com/p/protobuf/
对于 .NET,Jon Skeet 编写了一个端口:
http://code.google.com/ p/protobuf-net/
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/