找到你要的答案

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

Q:如何写一个序列化为一个HTTP的POST请求的身体吗?

I have a client-server app in which I have a Serializable class on both sides. I have to write an object of that class using ObjectOutputStream from the client and read it using ObjectInputStream on the server.

At the client I am using Apache HttpClient (version 4.2). I have to send the serializable object in an HttpPost request. How do I write the object to the HttpPost request in such a way that I can read it using ObjectInputStream at the server?

I am sending the request like this :

httpClient.execute(host,postRequest);

我有一个客户端-服务器应用程序中,我也对双方可序列化类。我写这个类的使用对象从客户端对象和服务器上使用对象输入流读它。

在客户端,我使用Apache HttpClient(版本4.2)。我不得不在HttpPost请求发送的可序列化的对象。我怎么写的对象在这样一种方式,我可以看它在服务器上使用了ObjectInPutStream HttpPost请求吗?

我发送这样的请求:

httpClient.execute(host,postRequest);
answer1: 回答1:

You need to use a BasicHttpEntityEnclosingRequest that contains a SerializableEntity.

Basically, it would look something like this:

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

你需要使用一个basichttpentityenclosingrequest包含serializableentity。

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

BasicHttpEntityEnclosingRequest postRequest = new BasicHttpEntityEnclosingRequest("POST", "uri");
postRequest.setEntity(new SerializableEntity(yourObject, false));
java  http-post  apache-httpclient-4.x