如何在 C# 中模拟 java 上的对象序列化

发布于 2024-10-09 04:02:25 字数 2048 浏览 8 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2024-10-16 04:02:25

我终于让它发挥作用了。

问题在于读取.NET 中的数据而不是使用 StreamReader,我需要立即使用 Stream 对象。无论如何,把它留在这里,以防它帮助其他人解决他们的问题:

错误的代码:

List<byte> lByte = new List<byte>();
using (StreamReader sr = new StreamReader(myRequest.GetResponse().GetResponseStream()))
{
    while (sr.Peek() >= 0)
    {
        lByte.Add((byte)sr.Read());
    }
}

正确的代码:

byte[] buffer = new byte[1000];
using (Stream sr = myRequest.GetResponse().GetResponseStream())
{
    sr.Read(buffer, 0, 1000);
}

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);
}
往昔成烟 2024-10-16 04:02:25

如果您可以控制 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文