1

I need to send an Object from client to server by serializing it.
This is my code:

     HttpURLConnection con = null;
     ObjectOutputStream out = null;
     ObjectInputStream inputStream = null;

     URL servlet = new URL("MY_URL");
        con = (HttpURLConnection) servlet.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);
        con.setRequestProperty("Content-type", "application/octet-stream");
        con.setRequestMethod("POST");
        out = new ObjectOutputStream(con.getOutputStream());
        out.writeObject(myobject);
        out.flush();
        out.close();

        inputStream = new ObjectInputStream(con.getInputStream());
        inputStream.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    finally
    {
    //  inputStream.close();
        con.disconnect();
    }
    return true;

Now, I am able to reach the Servlet, and I can retrieve the object through there.
The only problem is that as soon as I reach to this line:

inputStream = new ObjectInputStream(con.getInputStream());

I get an exception StreamCorruptedException, at the client side. (at the server side everything working great!) And if I take this line off, the servlet not being triggered (I mean the doGet() or doPost() not being called in the servlet)

What am I doing wrong?

This is the exact error:

06-02 12:41:53.549: WARN/System.err(4260): java.io.StreamCorruptedException
06-02 12:41:53.549: WARN/System.err(4260): java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2399)
06-02 12:41:53.549: WARN/System.err(4260): at java.io.ObjectInputStream.<init>(ObjectInputStream.java:447) 

Thanks,
Ray

Martijn Courteaux
  • 63,780
  • 43
  • 187
  • 279
rayman
  • 18,586
  • 41
  • 135
  • 234

2 Answers2

4

The client is expecting that the servlet writes an object back to the response something like:

ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(someObject);

But the servlet apparently actually doesn't write any object back. So the client should not decorate it with an ObjectInputStream. Just do so:

InputStream inputStream;
// ...
inputStream = connection.getInputStream();

or simply

connection.connect();

if you're not interested in the response anyway. The connection is executed on demand only. The getInputStream() will do that implicitly. That's why the request is not been fired until you call getInputStream(). Also see this answer for more hints.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • connection.connect(); , didnt trigger the other side but InputStream inputStream; // ... inputStream = connection.getInputStream(); did the job perfectly. thanks. – rayman Jun 02 '10 at 15:21
  • @BalusC Here i need to replace oos with input stream ah? am facing the same error and my code is ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream os = new ObjectOutputStream(baos);os.writeObject(serializable);os.reset();os.close(); – AndroidOptimist Mar 13 '14 at 10:00
0

Don't do this stuff yourself, look at HttpClient and spring's HttpInvoker.

Justin
  • 4,271
  • 4
  • 29
  • 52
  • It was a serious answer, I started off writing a client/server application exactly like the one above. And when I discovered HttpInvoker, I realized my folly; I would never go back. – Justin Jun 02 '10 at 14:25
  • Look at **Last Words** in BalusC's linked answer. – Justin Jun 02 '10 at 14:27
  • I counteracted the downvote since your intent is good. But you should in future post a bit more extended answer. Just *actually answer* the question and then close with the suggestion to use Apache HttpClient, if necessary along with code sample how you could do it with HttpClient. Right now you are not answering the core problem, even not how to do it with HttpClient. – BalusC Jun 02 '10 at 14:33
  • True it was perhaps too terse, but if you keep giving people the fish they are looking for, rather then they fish they need to catch; they are unlikely to eat for long. – Justin Jun 02 '10 at 20:03