Forums Register Login

Socket to HTTP

+Pie Number of slices to send: Send
Hi,
Is following senario possible?: If yes,how?
Java client(using java program) open SOCKET to Java Server(which consist of httpserver). Whenever java client send request to server, server must get request through HTTPREQUEST and send response back to client using HTTPRESPONSE and client should get response from server using SOCKET listener.
Thanks
Angela
P.S. Capital word are must for the senario.
+Pie Number of slices to send: Send
You can most definitely do that.
It will mean that you must learn the HTTP protocol, ie. you will be "manually" writing HTTP HEAD/GET/POST etc requests.
The server will of course take care of sending you back the correct HTTP response, you just read it and process it however you want.
Here's a nice resource for an introduction to the HTTP protocol.
http://www.jmarshall.com/easy/http/
+Pie Number of slices to send: Send
 

Originally posted by James Swan:

The server will of course take care of sending you back the correct HTTP response, you just read it and process it however you want.


How server will receive HTTP request & send HTTP response i.e using socket? I don't want to send HTTPresponse and receive HTTPRequest using socket interface on server side.
Thanks,
Angela
+Pie Number of slices to send: Send
Angela can you explain your requirements in more detail?
Are you responsible for only creating the client or do you also have to create the server?
My assumption was:
- you were creating a client to send HTTP requests to an existing HTTP/Application server
If this is the case then the server already knows how to process HTTP requests/responses and will be listening for them on a defined port (usually 80).
If you have to write the server yourself then it is a bit more involved, but that link I provided is still useful.
Please provide more details on what you're going to be doing.
James.
+Pie Number of slices to send: Send
 

Originally posted by James Swan:

Are you responsible for only creating the client or do you also have to create the server?


I have to create client only. I already have existing Java HTTP Server. All I have to do is to create client socket program to send request and get response from the existing HTTPServer.
I don't need to write any socket code on serverside to do HTTP request and HTTP response.
As it is HTTP server, it will do through HTTP
Am I right???

Thanks a lot,
Angela
+Pie Number of slices to send: Send
if you use raw socket you will have to form http request yourself.
you can use httpclient ( check apache jakarta commons httpclient ) to communicate from client side to the server without need of forming http request yourself. avoid using java.net default httpclient because it caches request content into buffer ( at least in jdk1.3 ), rather than establishing communication channel with server.
+Pie Number of slices to send: Send
Since you are only creating the client it is relatively straight forward.
Here are the high level steps that your client will need to perform:
1) Open a socket connection to your HTTP/Application server (eg www.myserver.com, port 80)
2) Write to the socket your HTTP request (see the 1st link I sent you), I'm assuming you're wanting to do something non-trivial, for example calling a servlet/jsp/cgi with some parameters, this is achieved by performing a GET or POST request (again see the link)
3) Read the response from the socket, the server "knows" HTTP and will respond appropriately
4) Process the "message" in the response
5) close the socket, done.
It is no different from any other client/server setup using sockets, the only difference is format of the messages being passed back and forth, in this case HTTP.
James.
+Pie Number of slices to send: Send
Thanks a lot I got it. I will write the following code on client side:
1) Open a socket connection to your HTTP/Application server (eg www.myserver.com, port 80)
2) Write to the socket your HTTP request (see the 1st link I sent you), I'm assuming you're wanting to do something non-trivial, for example calling a servlet/jsp/cgi with some parameters, this is achieved by performing a GET or POST request (again see the link)
3) Read the response from the socket, the server "knows" HTTP and will respond appropriately
4) Process the "message" in the response
5) close the socket, done.
On Serverside:
No code for socket. As it is HTTP Server.
Thanks,
Angela
+Pie Number of slices to send: Send
well, you will have to remember to strip of the headers @ the client end, off the response from the server. You will also have to make sure you are not behind a firewall on the client side.
Regds
Lupo
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2546 times.
Similar Threads
how to communicate Applet to Servet
socket read-write-read-write-...
Forward Socket to another server socket
Client-Server communication; client freezes
how do i capture screenshot via network in java swing
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:46:46.