How to send an HTTPS request by socket?

前端 未结 1 1199
谎友^
谎友^ 2021-01-16 22:20

I am learning about Java socket. I am trying to send an HTTPS GET request to Facebook, like this:

String request = \"GET / HTTP/1.0\\r\\n\"
                +         


        
1条回答
  •  一整个雨季
    2021-01-16 23:10

    Two answers.

    1. You are trying to talk to an HTTPS server. That requires you to establish an SSL/TLS channel. That is >>really difficult<< to do on a plain socket ... you'd have to implement the connection negotiation, the client-side validation of the key chain, the session encryption / decryption, and so on.

      A more sane way would be to use SSLSocket, SSLSocketFactory and so on to handle the SSL/TLS stuff ... but it is still a bit tricky.

    2. Talking to an HTTP or HTTPS service over a Socket is not a sensible approach. You are not really going to learn much by doing it, because the normal way to talk to an HTTP / HTTPS service from Java is to use HttpURLConnection, or the Apache HTTP libraries if you want more control.

    0 讨论(0)
提交回复
热议问题