Bonjour,
Dans mon application c# un bouton qui permet l'upload des fichiers dans un serveur FTP, pour celà j'utilise la classe en pièce-jointe.
les informations du serveur FTP sont dans le web.config:
Je commence dans un premier temps par recupérer ces informations depuis le fichier de configuration puis j'utilise la fonction FTP.FtpLogin(); de la classe FTP (en pièce-jointe).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 <FtpConfigClass ftpServer="xx.xx.xx.xx" ftpUser="user_upload" ftpPassword="a_123456" ftpPort="21" bufferSize="512" timeOut="10"/>
Cette fonction fait appel à readResponse(); qui permet de récupérer la réponse du serveur grace à ParseHostResponse();.
dans les 3/4 des cas le serveur ne m'envoi aucune réponses
FtpLogin:
readResponse:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public void FtpLogin() { //check if the connection is currently open if (_isLoggedIn) { //its open so we need to close it CloseConnection(); } //message that we're connection to the server Debug.WriteLine("Opening connection to " + _ftpServer, "FtpClient"); //create our ip address object IPAddress remoteAddress = null; //create our end point object IPEndPoint addrEndPoint = null; try { //create our ftp socket ftpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //retrieve the server ip remoteAddress = Dns.GetHostEntry(_ftpServer).AddressList[0]; //set the endpoint value addrEndPoint = new IPEndPoint(remoteAddress, _ftpPort); //connect to the ftp server ftpSocket.Connect(addrEndPoint); } catch (Exception ex) { // since an error happened, we need to //close the connection and throw an exception if (ftpSocket != null && ftpSocket.Connected) { ftpSocket.Close(); } throw new FtpException("Couldn't connect to remote server", ex); } //read the host response readResponse(); //check for a status code of 220 if (_statusCode != 220) { //failed so close the connection CloseConnection(); //throw an exception throw new FtpException(result.Substring(4)); } //execute the USER ftp command (sends the username) Execute("USER " + _ftpUsername); //check the returned status code if (!(_statusCode == 331 || _statusCode == 230)) { //not what we were looking for so //logout and throw an exception LogOut(); throw new FtpException(result.Substring(4)); } //if the status code isnt 230 if (_statusCode != 230) { //execute the PASS ftp command (sends the password) Execute("PASS " + _ftpPassword); //check the returned status code if (!(_statusCode == 230 || _statusCode == 202)) { //not what we were looking for so //logout and throw an exception LogOut(); throw new FtpException(result.Substring(4)); } } //we made it this far so we're logged in _isLoggedIn = true; //verbose the login message Debug.WriteLine("Connected to " + _ftpServer, "FtpClient"); //set the initial working directory ChangeWorkingDirectory(_ftpPath); }
ParseHostResponse:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 private void readResponse() { statusMessage = ""; result = ParseHostResponse(); _statusCode = int.Parse(result.Substring(0, 3)); }
Quelqu'un à une idée ?
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 private string ParseHostResponse() { //while (true) //{ // //retrieve the host response and convert it to // //a byte array // bytes = ftpSocket.Receive(buffer, buffer.Length, 0); // //decode the byte array and set the // //statusMessage to its value // statusMessage += ASCII.GetString(buffer, 0, bytes); // //check the size of the byte array // if (bytes < buffer.Length) // { // break; // } //} do { try { bytes += ftpSocket.Receive(buffer, 0 + bytes, buffer.Length - bytes, SocketFlags.None); statusMessage += ASCII.GetString(buffer, 0, bytes); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.WouldBlock || ex.SocketErrorCode == SocketError.IOPending || ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { // socket buffer is probably empty, wait and try again Thread.Sleep(30); } else throw ex; // any serious error occurr } } while (ftpSocket.Available > 0); //split the host response string[] msg = statusMessage.Split('\n'); //check the length of the response if (statusMessage.Length > 2) statusMessage = msg[msg.Length - 2]; else statusMessage = msg[0]; //check for a space in the host response, if it exists return //the message to the client if (!statusMessage.Substring(3, 1).Equals(" ")) return ParseHostResponse(); //check if the user selected verbose Debugging if (_doVerbose) { //loop through the message from the host for (int i = 0; i < msg.Length - 1; i++) { //write each line out to the window Debug.Write(msg[i], "FtpClient"); } } //return the message return statusMessage; }
Merci d'avance
Partager