In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to analyze C# FTP WebRequest objects. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
C # FTP WebRequest object
Microsoft's .NET framework 2.0 adds support for FTP compared to 1.x. In the past, in order to meet my needs, I didn't have to use third-party class libraries to implement FTP functionality, but for reliability, it was better to use .NET framework classes. My code is not in the form of a reusable class library, but it is easier to understand and meets your needs. It can upload, download, delete and other functions. Later in this article, I will show you the simple code to implement FTP in .NET 2.0, using c #. Perhaps because this is a new class in .NET, or because third-party libraries have been able to meet your needs well, this part of the class library of .NET 2.0 has not received enough attention.
Background
As part of my work, I have used the FTP module, but I can only use it in .NET 1.1, so I can't delve into the implementation of FTP under .NET 2.0. But I believe that the support for FTP under .NET 2.0 is very good.
Code
Don't forget to introduce namespaces
Using System.NET
Using System.IO
The following steps include the general process of using the FTP WebRequest class to implement the FTP function
1. Create a FTP WebRequest object that points to the uri of the FTP server
2. Set the execution method of FTP (upload, download, etc.)
3. Set properties to the FTP WebRequest object (whether ssl is supported, whether binary transport is used, etc.)
4. Set login authentication (username, password)
5. Execute the request
6. Receive the corresponding stream (if necessary)
7. Close the FTP request if there is no open stream
Developing any FTP application requires a relevant FTP server and its configuration information. FTP WebRequest exposes some properties to set this information.
The next code example shows the upload function, first setting up a uri address, including the path and file name. This uri is used in the FTP WebRequest instance.
Then set the properties of the C # FTP WebRequest object according to the FTP request
Some of the important attributes are as follows:
◆ Credentials-specifies the username and password to log in to the FTP server.
◆ KeepAlive-specifies whether the connection should be closed or after the request is completed. Default is true.
◆ UseBinary-specifies the type of file transfer. There are two file transfer modes, one is Binary and the other is ASCII. When the two methods are transmitted, the 8th bit of the byte is different. ASCII uses bit 8 as error control, while the 8 bits of Binary make sense. So be careful when you use ASCII transport. In a nutshell, if files that can be read and written in notepad can be transferred using ASCII, others must use Binary mode. Of course, it is also very good to use Binary mode to send ASCII files.
◆ UsePassive-specifies whether to use active mode or passive mode. Previously, all clients used active mode and worked well, but now some ports will be closed because of the client firewall, so active mode will fail. Passive mode is used in this case, but some ports may also be blocked by the server's firewall. However, because FTP servers need their FTP services to connect to a certain number of clients, they always support passive mode. This is why we use passive mode, which is obviously better than active mode in order to ensure that data can be transmitted correctly. (translator's note: the establishment of a data transfer channel in PORT mode is initiated by the server, which uses port 20 to connect to a port greater than 1024 of the client; in PASV mode, the establishment of a data transfer channel is initiated by the FTP client, which uses a port greater than 1024 to connect to a port above 1024 of the server.)
◆ ContentLength-setting this property is useful for the FTP server, but the client does not use it because FTP WebRequest ignores it, so it is invalid in this case. But if we set this property, the FTP server will predict the size of the file in advance (as is the case with upload)
◆ Method-specifies what command the current request is (upload,download,filelist, etc.). This value is defined in the structure WebRequestMethods.FTP. The above describes the C # FTP WebRequest object.
Private void Upload (string filename) {
FileInfo fileInf = new FileInfo (filename)
String uri = "ftp://" + ftpServerIP +" / "+ fileInf.Name
FtpWebRequest reqFTP
/ / create a FtpWebRequest object based on uri
ReqFTP = (FtpWebRequest) FtpWebRequest.Create (new Uri
("ftp://" + ftpServerIP +" / "+ fileInf.Name))
/ / ftp username and password
ReqFTP.Credentials = new NetworkCredential (ftpUserID, ftpPassword)
/ / default is true, and the connection will not be closed
/ / executed after a command
ReqFTP.KeepAlive = false
/ / specify what command to execute
ReqFTP.Method = WebRequestMethods.Ftp.UploadFile
/ / specify the data transfer type
ReqFTP.UseBinary = true
/ / notify the server of the file size when uploading the file
ReqFTP.ContentLength = fileInf.Length
/ / set the buffer size to 2kb
Int buffLength = 2048
Byte [] buff = new byte [buffLength]
Int contentLen
/ / Open a file stream (System.IO.FileStream) to read the uploaded file
FileStream fs = fileInf.OpenRead ()
Try
{
/ / write the uploaded file to the stream
Stream strm = reqFTP.GetRequestStream ()
/ / 2kb of each file stream read
ContentLen = fs.Read (buff, 0, buffLength)
/ / the stream content does not end.
While (contentLen! = 0)
{
/ / write the content from file stream to upload stream
Strm.Write (buff, 0, contentLen)
ContentLen = fs.Read (buff, 0, buffLength)
}
/ / close two streams
Strm.Close ()
Fs.Close ()
}
Catch (Exception ex)
{
MessageBox.Show (ex.Message, "Upload Error")
}
}
On how to analyze the C# FTP WebRequest object to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.