Skip to content

Instantly share code, notes, and snippets.

@cy6erGn0m
Created June 8, 2012 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cy6erGn0m/2894017 to your computer and use it in GitHub Desktop.
Save cy6erGn0m/2894017 to your computer and use it in GitHub Desktop.
Simplest HTTP request example
package cg;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author Sergey Mashkov aka cy6erGn0m
* @since 08.06.12
*/
public class Main {
public static void main(String[] args) throws Exception {
Socket s = new Socket(InetAddress.getByName("konn.ohooligans.ru"), 80);
try {
OutputStreamWriter w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
w.write("GET /bar HTTP/1.1\n");
w.write("Host: konn.ohooligans.ru\n");
w.write("Connection: close\n\n");
w.flush();
InputStream is = s.getInputStream();
int rc;
byte[] buff = new byte[256];
while ((rc = is.read(buff)) != -1) {
System.out.write(buff, 0, rc);
}
} finally {
s.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment