Skip to content

Instantly share code, notes, and snippets.

@cy6erGn0m
Created Jun 8, 2012
Embed
What would you like to do?
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