I have a Main() class where I serialize an object of a class called Names. I am using Apache HttpClient‘s HttpPost() to call a servlet.
public static void main(String[] args) {
Names names = new Names();
names.setName("ABC");
names.setPlace("Bangalore");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Name.txt"));
out.writeObject(names);
out.close();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:6080/HttpClientGson/FirstHttpPostServlet");
Now, how do I send the ObjectOutputStream object? I wrote the following line
httppost.setEntity(out)
But setEntity() can only take objects of HttpEntity type. Is there any other method of HttpClient that I can use to send serialized object?
You could
SerializableEntityclass shipped with HttpClientPlease note, though, that binary object serialization should be used only when absolutely required. Other serialization formats such as XML or JSON should generally be preferred.