Bonjour,

J'aurai besoin d'un petit coup de main car je ne comprend pas pourquoi ce code ne marche pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
	/* variable globale de ma classe */
	static private String adr;
	static private int port;
	static private SocketChannel sockChan;
	static private InetSocketAddress remote;
	static private ByteBuffer buffReq;
	static private StringBuffer sb;
	static private Path pathFichierRep;
 
	/* Méthode de connexion au serveur */
	public static boolean seConnecter(String adr, int port) throws IOException {
		remote = new InetSocketAddress(adr, port);
		sockChan = SocketChannel.open(remote);
		return sockChan.isOpen();
	}
 
	/* Méthode d'envoi d'une requete http enregistrée dans un fichier texte */
	public static int sendRequeteFromFile(String path) throws IOException {
		int tailleChan = -1;
 
		if (sockChan.isOpen()) {
			FileChannel in = null; // canal d'entrée		    
			try {
				/* ouverture et lecture du fichier vers le buffer */
				in = FileChannel.open(FileSystems.getDefault().getPath(path), StandardOpenOption.READ);
				tailleChan = new Long(in.size()).intValue();
				buffReq = ByteBuffer.allocate(tailleChan);
				in.read(buffReq);
 
				String req = new String(buffReq.array());
				System.out.println(req);
 
				buffReq = ByteBuffer.wrap(req.getBytes("US-ASCII"));
 
				sockChan.write(buffReq);
			} finally {
				if(in != null) {
					in.close();
				}
			}
		}
 
		return tailleChan;
	}
Je m'explique...

Mon objectif est d'envoyer une requête GET, POST... à un serveur HTTP et de récupérer la réponse dans un 2e temps.

Lorsque j'envoie une requête avec cette méthode, je reçois comme réponse du serveur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
HTTP/1.1 408 Request Time-out
Date: Wed, 30 Oct 2013 15:25:52 GMT
Server: Apache
Vary: Accept-Encoding
Content-Length: 21
Connection: close
Content-Type: text/html; charset=iso-8859-1
 
HTTP_REQUEST_TIME_OUT
Alors que si je modifie mon code avec la ligne ci-dessous, je reçois une réponse correcte du serveur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
String req = "GET ma requete get";
Je n'arrive pas à trouver de solution à mon problème. J'ai pourtant essayé plusieurs méthodes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	public static int sendRequeteFromFile(String path) throws IOException {
		int tailleChan = -1;
 
		if (sockChan.isOpen()) {
			FileChannel in = null; // canal d'entrée
			try {
				/* ouverture et lecture du fichier vers le buffer */
				in = FileChannel.open(FileSystems.getDefault().getPath(path), StandardOpenOption.READ);
				tailleChan = new Long(in.size()).intValue();
 
				in.transferTo(0, tailleChan, sockChan);
			} finally {
				if(in != null) {
					in.close();
				}
			}
		}
 
		return tailleChan;
	}
Même résultat!

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	public static int sendRequeteFromFile(String path) throws IOException {
		int tailleChan = -1;
 
		if (sockChan.isOpen()) {
			FileChannel in = null; // canal d'entrée
			try {
				/* ouverture et lecture du fichier vers le buffer */
				in = FileChannel.open(FileSystems.getDefault().getPath(path), StandardOpenOption.READ);
				tailleChan = new Long(in.size()).intValue();
 
				buffReq = ByteBuffer.allocate(tailleChan);
				in.read(buffReq);
				buffReq.flip();
 
				sockChan.write(buffReq);
			} finally {
				if(in != null) {
					in.close();
				}
			}
		}
Même résultat!

Je ne comprends vraiment pas pourquoi ça ne marche pas.
Est-ce que quelqu'un aurait une solution s'il vous plait ?