En cours de rédaction...
Exemple [voir]package nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class ClientHTTP {
public static void main(String args[]) {
String hote = "www.laltruiste.com";
SocketChannel sc = null;
try {
Charset latin = Charset.forName("ISO-8859-1");
CharsetDecoder decodeur = latin.newDecoder();
CharsetEncoder encodeur = latin.newEncoder();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
CharBuffer cb = CharBuffer.allocate(1024);
InetSocketAddress adresse = new InetSocketAddress(hote, 80);
sc = SocketChannel.open();
sc.connect(adresse);
String request =
"POST /cgi-bin/htsearch HTTP/1.1\r\n" +
"Host: " + hote + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; fr; rv:1.5) "
+ "Gecko/20031007 Firebird/0.7\r\n" +
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
+ "text/plain;q=0.8,video/x-mng,image/png,image/jpeg,"
+ "image/gif;q=0.2,*/*;q=0.1\r\n" +
"Accept-Language: fr,en;q=0.5\r\n" +
"Accept-Encoding: gzip,deflate\r\n" +
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
"Keep-Alive: 300\r\n" +
"Connection: keep-alive\r\n" +
"Referer: http://www.laltruiste.com/accueil.php?compteur=1&evolution=5\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 117\r\n\r\n" +
"words=java&method=and&format=builtin-long&sort=score&choix=site"
+ "&config=laltruiste_htdig&restrict=&exclude=&evolution=\r\n";
sc.write(encodeur.encode(CharBuffer.wrap(request)));
while ((sc.read(bb)) != -1) {
bb.flip();
decodeur.decode(bb, cb, false);
cb.flip();
System.out.println(cb);
bb.clear();
cb.clear();
}
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (sc != null) {
try {
sc.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
} |