1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public static void main(String[] args) throws IOException {
Socket s = new Socket("www.developpez.com", 80);
//recupération des flux
OutputStream oStream = s.getOutputStream();;
InputStream iStream = s.getInputStream();;
byte[] b = new byte[1000];
String g = "GET / HTTP/1.1\n" + "Host: www.developpez.com\n\n";
try {
oStream.write(g.getBytes());
int bitsRecus = 0;
while((bitsRecus = iStream.read(b)) >= 0) {
System.out.println("On a recu : " + bitsRecus + " bits");
System.out.println("Recu : " + new String(b, 0, bitsRecus));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//fermeture des flux et des sockets
oStream.close();
iStream.close();
s.close();
}
} |