Created
April 4, 2016 15:55
-
-
Save mr337/20c63ecd2f757604ab45cf79c2f8e3f4 to your computer and use it in GitHub Desktop.
Java SSL client tester
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.net.*; | |
| import java.io.*; | |
| import java.security.*; | |
| import javax.net.ssl.*; | |
| public class HTTPSClient { | |
| public static void main(String[] args) { | |
| if (args.length == 0) { | |
| System.out.println("Usage: java HTTPSClient host"); | |
| return; | |
| } | |
| int port = 8443; // default https port | |
| String host = args[0]; | |
| try { | |
| Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); | |
| SSLSocketFactory factory | |
| = (SSLSocketFactory) SSLSocketFactory.getDefault(); | |
| SSLSocket socket = (SSLSocket) factory.createSocket(host, port); | |
| Writer out = new OutputStreamWriter(socket.getOutputStream()); | |
| // https requires the full URL in the GET line | |
| out.write("GET http://" + host + "/ HTTP/1.1\r\n"); | |
| out.write("\r\n"); | |
| out.flush(); | |
| // read response | |
| BufferedReader in = new BufferedReader( | |
| new InputStreamReader(socket.getInputStream())); | |
| int c; | |
| while ((c = in.read()) != -1) { | |
| System.out.write(c); | |
| } | |
| out.close(); | |
| in.close(); | |
| socket.close(); | |
| } | |
| catch (IOException e) { | |
| System.err.println(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment