Featured post
How can you emulate object serialization on java in C# -
i need call servlet call automation of java applet using c#. java applet calls servlet using 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 need simulate using c#. believe counterpart object 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 data. newstream.write(param[0],0,param[0].length); newstream.write(param[1],0,param[1].length); newstream.close();
how write string serialized java string? there workaround here? according documentation of objectoutputstream in java, serialize object except primitive type. know string class, serialzie object or special case?
i have tried 1 solution, have imported ikvm (http://www.ikvm.net/) java virtual machine in reference , trying use java.io library in java. unforunately, when objectinputstream constructor called, "invalid stream header" thrown.
here altered code:
myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.contentlength = data.length; stream newstream = myrequest.getrequeststream(); // send 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 objectinputstream object inputstream = new objectinputstream(new bytearrayinputstream(barr)); object obj = null; while ((obj = inputstream.readobject()) != null) { string objstr = obj string; } } catch (java.lang.exception ex)
if have control on serialization/deserialization on java side, best bet use cross-platform serialization protocol such protocol buffers. c++, java, , python:
http://code.google.com/p/protobuf/
for .net, jon skeet wrote port:
- Get link
- X
- Other Apps
Comments
Post a Comment