How can you establish a TCP connection by yourself, in other words, how is it possible to send a HTTP request and receive the HTTP reply from a server using java language? its relevant to say that this should be done without using an http library.
Asked
Active
Viewed 106 times
-2
-
1https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html – tkausl Oct 20 '20 at 20:05
-
1Does this answer your question? [How to use java.net.URLConnection to fire and handle HTTP requests?](https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests) – Sean Powell Oct 20 '20 at 20:05
-
To do this without HTTP Library it is possible but can get a hell of work if you will try to handle all topics like Transfer-Encoding, Keep-Alive, Chunking etc... Why don't you want to use http library is this maybe an homework task from school? Then the teacher will expect simple GET / HTTP1.0 as request and remove the part above the first empty line. – SkateScout Oct 21 '20 at 12:18
1 Answers
0
Here is an simple sample.
try(final Socket s = new Socket("localhost", 80);
final InputStream i = s.getInputStream();
final OutputStream o = s.getOutputStream()) {
}

SkateScout
- 815
- 14
- 24
-
I can´t use any http libraries because this is a condition of the school project. thanks for all the replies. I'm beginning the study of networking and the essay is about devellop a webclient. - you are supposed to establish the TCP connection by yourself, send the HTTP request and receive the HTTP reply; - the HTTP reply should then be presented to the user; - prepare your client to proactively act to the different HTTP replies. I'm a bit confused to do all this operations in java. thanks in advance for any help. – Mário Friande Oct 21 '20 at 17:28