Created
October 8, 2021 13:34
-
-
Save devtdeng/19ad7adac5255848ec971d20235ed21b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import javax.net.ssl.*; | |
/* | |
* This example demostrates how to use a SSLSocket as client to | |
* send a HTTP request and get response from an HTTPS server. | |
* It assumes that the client is not behind a firewall | |
* https://docs.oracle.com/javase/10/security/sample-code-illustrating-secure-socket-connection-client-and-server.htm | |
* | |
* Usage: | |
* java -Djavax.net.ssl.trustStorePassword="password" \ | |
* -Djavax.net.ssl.trustStore=myTrustStore \ | |
* -Djavax.net.ssl.keyStorePassword="password" \ | |
* -Djavax.net.ssl.keyStore=myKeyStore.jks \ | |
* SSLSocketClient <IP> <port> | |
*/ | |
public class SSLSocketClient { | |
public static void main(String[] args) throws Exception { | |
String host = null; | |
int port = -1; | |
for (int i = 0; i < args.length; i++) | |
System.out.println(args[i]); | |
if (args.length < 2) { | |
System.out.println( | |
"USAGE: java SSLSocketClient " + | |
"host port"); | |
System.exit(-1); | |
} | |
try { | |
host = args[0]; | |
port = Integer.parseInt(args[1]); | |
} catch (IllegalArgumentException e) { | |
System.out.println("USAGE: java SSLSocketClient " + | |
"host port"); | |
System.exit(-1); | |
} | |
try { | |
SSLSocketFactory factory = | |
(SSLSocketFactory)SSLSocketFactory.getDefault(); | |
SSLSocket socket = | |
(SSLSocket)factory.createSocket(host, port); | |
/* | |
* send http request | |
* | |
* Before any application data is sent or received, the | |
* SSL socket will do SSL handshaking first to set up | |
* the security attributes. | |
* | |
* SSL handshaking can be initiated by either flushing data | |
* down the pipe, or by starting the handshaking by hand. | |
* | |
* Handshaking is started manually in this example because | |
* PrintWriter catches all IOExceptions (including | |
* SSLExceptions), sets an internal error flag, and then | |
* returns without rethrowing the exception. | |
* | |
* Unfortunately, this means any error messages are lost, | |
* which caused lots of confusion for others using this | |
* code. The only way to tell there was an error is to call | |
* PrintWriter.checkError(). | |
*/ | |
socket.startHandshake(); | |
PrintWriter out = new PrintWriter( | |
new BufferedWriter( | |
new OutputStreamWriter( | |
socket.getOutputStream()))); | |
out.println("GET / HTTP/1.0"); | |
out.println(); | |
out.flush(); | |
/* | |
* Make sure there were no surprises | |
*/ | |
if (out.checkError()) | |
System.out.println( | |
"SSLSocketClient: java.io.PrintWriter error"); | |
/* read response */ | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader( | |
socket.getInputStream())); | |
String inputLine; | |
while ((inputLine = in.readLine()) != null) | |
System.out.println(inputLine); | |
in.close(); | |
out.close(); | |
socket.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment