0

The code below doesn't get any html from the url

URL url = new URL("https://google.com");
        Socket socket = new Socket(url.getHost(),443);
        socket.setSoTimeout(15000);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
        String c = "";
        while((c = reader.readLine())!=null)
        {
            System.out.print(c);
        }
        reader.close();
        socket.close();

I'm aware of URLConnection method openConnection() and URL class method openStream() but i don't want to use those. All i want to know is why i don't get any input using Socket class but i do get an input using other approaches.

1 Answer 1

2

You're not sending any request. HTTP is a request-response protocol: you need to send a request telling the Web server which URL you want to retrieve, and then it will send you the data for this URL.

Sign up to request clarification or add additional context in comments.

3 Comments

how can i do that using socket?
Learn about how the HTTP protocol works, find out the format of the request, and send that request through the socket. (Also you probably want to send HTTP requests to port 80, not HTTPS requests to port 443, because implementing a HTTPS handshake over raw sockets is rather non-trivial.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.