Skip to content

Instantly share code, notes, and snippets.

@geunho
Created November 7, 2019 05:08
Show Gist options
  • Save geunho/7087a5191914d3dc6ac5ce886beb59ba to your computer and use it in GitHub Desktop.
Save geunho/7087a5191914d3dc6ac5ce886beb59ba to your computer and use it in GitHub Desktop.
Simple http client in Java
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