The ftpwebrequest class implements the FTP function.
1. Create an ftpwebrequest object pointing to the FTP server URI
2. Set the FTP execution method (upload, download, etc)
3. Set attributes for the ftpwebrequest object (whether to support SSL or binary transmission)
4. Set logon authentication (user name and password)
5. execute the request
6. Receive the corresponding stream (if needed)
7. If no stream is opened, disable the FTP request.
Some important attributes are as follows:
· Credentials-specifies the username and password used to log on to the FTP server.
· Keepalive-specifies whether the connection should be closed or closed after the request is completed. The default value is true.
· Usebinary-specifies the file transfer type. There are two file transmission modes: Binary and ASCII. When the two methods are used for transmission, the 8th-bit bytes are different. ASCII uses 8th bits as error control, while binary's 8 bits make sense. So be careful when using ASCII transmission. To put it simply, it is safe to transmit files that can be read and written using notepad in ASCII mode, while others must use binary mode. Of course, it is also very good to use binary mode to send ASCII files.
· Usepassive-specifies whether the active or passive mode is used. Early on, all clients used active mode and work well. Now, some ports will be closed because of the existence of the Client Firewall, so that the active mode will fail. In this case, the passive mode is required, but some ports may also be blocked by the server's firewall. However, because the FTP server requires its FTP service to connect to a certain number of clients, they always support the passive mode. This is why we need to use the original intent of the passive mode. To ensure that data can be transmitted correctly, the passive mode is obviously better than the active mode. (Note: In active (port) mode, a data transmission channel is initiated by the server. The server uses Port 20 to connect to a port greater than 1024 of the client. In passive (PASV) in this mode, the establishment of the data transmission channel is initiated by the FTP client, which uses a port greater than 1024 to connect to a port greater than 1024 of the server)
· Contentlength-setting this attribute is useful for the FTP server, but is not used by the client. Because ftpwebrequest ignores this attribute, this attribute is invalid in this case. However, if this attribute is set, the FTP server will predict the file size in advance (this will happen during upload)
· Method-specifies the command (upload, download, filelist, etc.) for the current request ). This value is defined in the struct webrequestmethods. FTP.
When you encounter: "remote server return error: (550) file unavailable (for example, file not found, file cannot be accessed)", it may be due to the following reasons:
1. the URL path is incorrect. check whether there are more spaces or case-sensitive issues.
2. Is the permission adequate?
3. When repeated connections are required, such as getfilelist, all files need to be obtained recursively, keepalive is set to false, and a query request is closed after completion.