Skip to content

Instantly share code, notes, and snippets.

@lnicola
Created June 20, 2014 15:50
Show Gist options
  • Save lnicola/90e1de002d6302b72f03 to your computer and use it in GitHub Desktop.
Save lnicola/90e1de002d6302b72f03 to your computer and use it in GitHub Desktop.
small Java HTTP client
package httpclient;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class HttpClient {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: httpclient <url>");
return;
}
try {
URL url = new URL(args[0]);
URLConnection client = url.openConnection();
InputStream inputStream = client.getInputStream();
byte[] buf = new byte[1024];
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