Skip to content

Instantly share code, notes, and snippets.

@calimaborges
Last active November 16, 2016 15:58
Show Gist options
  • Save calimaborges/b07b26bc3efa9e28e7a77bbfd410bbcd to your computer and use it in GitHub Desktop.
Save calimaborges/b07b26bc3efa9e28e7a77bbfd410bbcd to your computer and use it in GitHub Desktop.
Simple HTTP Client
package carlosborges.httpclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class App {
public static void main( String[] args ) throws IOException {
Socket echoSocket = new Socket("localhost", 4567);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
out.print("GET / HTTP/1.1\r\n");
out.print("Host: localhost\r\n");
out.print("Accept: text/plain\r\n");
out.print("\r\n\r\n");
out.flush();
String linha;
while ((linha = in.readLine()) != null) {
System.out.println(linha);
}
out.close();
in.close();
echoSocket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment