Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can object send through HTTP protocol?  RSS feed

 
cb poo
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a client API that will send user object to server using HTTP protocol. A Listener Daemon program will listen to port 80 and base on different incoming type of object, it will determine the next step to do.
Is this workable, allow ???
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cb poo:
I have a client API that will send user object to server using HTTP protocol. A Listener Daemon program will listen to port 80 and base on different incoming type of object, it will determine the next step to do.
Is this workable, allow ???


HTTP is a text based protocol, so to send objects you will need to serialize them into text. There are tools that will serialize java objects to XML.
 
cb poo
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then for my server program, how detect different type of object after serialize?
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cb poo:
Then for my server program, how detect different type of object after serialize?

It really just depends on how you decide to implement it. Say you want to pass in a Car object with a capacity and color, you could have the client send the following XML as a String:


When the server receives this, it can parse it using JDOM (or whatever suits) and extract all the relevant information easily, including object type. You could write the code to do all this conversion yourself, or use any of the standard APIs, whichever you're more comfortable with.
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can serialize a Java object across HTTP using java.net.URLConnection. Get the URLConnection's output stream and wrap it with a java.io.ObjectOutputStream. As for determining what object you have on the server side, it isn't the best design or most efficient method, but you could always use the instanceof operator.
 
cb poo
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks like XML is a better and flexible approach. Then I will take this. Thanks guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!