如何将 Serializable 编写为 http post 请求的主体?

How to write a Serializable as the body of an http post request?

我有一个客户端-服务器应用程序,其中两边都有 Serializable class。我必须使用 client 中的 ObjectOutputStream 编写 class 的对象,并使用服务器上的 ObjectInputStream 读取它。

在客户端,我使用的是 Apache HttpClient(4.2 版)。我必须在 HttpPost 请求中发送可序列化对象。如何将对象写入 HttpPost 请求,以便我可以在服务器上使用 ObjectInputStream 读取它?

我正在发送这样的请求:

httpClient.execute(host,postRequest);

您需要使用 BasicHttpEntityEnclosingRequest that contains a SerializableEntity

基本上,它看起来像这样:

BasicHttpEntityEnclosingRequest postRequest = new BasicHttpEntityEnclosingRequest("POST", "uri");
postRequest.setEntity(new SerializableEntity(yourObject, false));