Created
November 7, 2019 05:08
-
-
Save geunho/7087a5191914d3dc6ac5ce886beb59ba to your computer and use it in GitHub Desktop.
Simple http client in Java
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.InputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
public class HttpClient { | |
public static void main(String[] args) { | |
if (args.length == 0) { | |
System.err.println("Url must be specified."); | |
return; | |
} | |
try { | |
URL url = new URL(args[0]); | |
URLConnection client = url.openConnection(); | |
InputStream inputStream = client.getInputStream(); | |
byte[] buf = new byte[2048]; | |
for (int len; (len = inputStream.read(buf)) > 0;) { | |
System.out.write(buf, 0, len); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(System.err); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment