Brain-damaged ftpwebrequest 2 Accident Scene Reproduction

Source: Internet
Author: User

Since the previous post is so abstract that many people cannot understand itCodeLet's review the scene of the accident.

First, install FTP software on the local machine. I use Serv-U, a widely used FTP server, to prepare the FTP directory. Here I use my own MP3 directory.

There is a subdirectory 2.

We can see that the contents of the two directories are completely different so that we can reproduce the scene of the accident.

The second part is to create a winform project.

Then, use three buttons on the interface to implement testcase.

 

 

 

The third step is the preparation. We have prepared an FTP operation class:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. net; using system. io; namespace testftpwebrequest {class ftphelper {public static void upload (string URL, string localpath) {fileinfo F = new fileinfo (localpath); ftpwebrequest Req = (ftpwebrequest) ftpwebrequest. create (URL + "/" + F. name); req. credentials = new networkcredential ("test", "123456"); req. met Hod = webrequestmethods. FTP. uploadfile; req. usebinary = true; req. contentlength = f. length; ftpwebresponse rep = (ftpwebresponse) req. getresponse (); Using (Stream S = req. getrequeststream () {using (filestream FS = f. openread () {int readcount = 0; long totalread = 0; byte [] buffer = new byte [1024]; while (true) {readcount = FS. read (buffer, 0, 1024); totalread + = readcount; S. write (buffer, 0, re Adcount); If (totalread> = f. length) {break ;}}}} public static string [] list (string URL) {list <string> rs = new list <string> (); ftpwebrequest Req = (ftpwebrequest) ftpwebrequest. create (URL); req. credentials = new networkcredential ("test", "123456"); req. method = webrequestmethods. FTP. listdirectory; ftpwebresponse rep = (ftpwebresponse) req. getresponse (); long Len = rep. contentlength; using (Stream S = rep. getresponsestream () {memorystream MS = new memorystream (); int readcount = 0; long totalcount = 0; byte [] buffer = new byte [1024]; while (true) {readcount = S. read (buffer, 0, 1024); totalcount + = readcount; Ms. write (buffer, 0, readcount); If (totalcount> = Len) {break;} s. close (); rep. close (); string STR = encoding. default. getstring (Ms. toarray (); stringreader sr = New String Reader (STR); string line = string. Empty; while (line = Sr. Readline ())! = NULL) {Rs. Add (line) ;}return Rs. toarray ();}}}

Here we have prepared two operations: Reading the file list and uploading files.

Finally, write the call method in the button.

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; namespace testftpwebrequest {public partial class form1: FORM {const string path1 = @ "D: \ mydocument \ test.jpg"; const string path2 = @ "D: \ mydocument \ yoxi.jpg "; const string ftpurl = "ftp: // 192.168.212.102"; const string FTPU Rl2 = "ftp: // 192.168.212.102/2"; Public form1 () {initializecomponent ();} private void button3_click (Object sender, eventargs e) {textbox1.text = string. empty; string [] files = ftphelper. list (ftpurl); textbox1.text = "file count:" + files. length + "\ r \ n"; foreach (string s in files) {textbox1.text + = "ftp:" + S + "\ r \ n ";}} private void button#click (Object sender, eventargs e) {ftphelper. upl Oad (ftpurl2, path1); MessageBox. Show ("up OK! ");} Private void button2_click (Object sender, eventargs e) {ftphelper. Upload (ftpurl2, path2); MessageBox. Show (" up OK! ");}}}

Well, according to the two FTP methods, we can see that the two methods should be independent on the surface, no matter when, if I press the list button, all the files in the FTP root directory should be returned. However, we will wait and see the actual execution results.

First, executeProgramClick the list with no upload button. At this time, we can see that the returned file is indeed the file in the root directory.

 

Now, click "firstupload". How can I guess the result? Error:

The path is correct. Why is an error reported? You can think about it yourself.

Next we will change the order. Run the program again. Click firstupload this time. The result is uploaded successfully. The result is as follows:

Okay. At this time, click the list button to display the content of the root directory, but the result is displayed:

Do you understand this? Next, Let's adjust the FTP settings. The FTP account settings are as follows:

Now we can remove the check box that will lock the user to the main directory:

 

 

 

 

Now, let's execute the program again.

Click the list first, and then click upload to see if there is no error. The operation is successful:

---------------------------------------------------------

At first, I suspected it was because keepalive kept the link, because the result is very similar that every request has reused the connection. Therefore, keepalive = false was added to both methods.

Then, restore the Serv-U settings and lock the user to the check box of the main directory.

Execute the program, click the list, and then click upload. The operation is successful. It seems that there is no problem. But don't be too happy. At this time, click the list again to see the error:

So far, I should be clear about the cause and effect of the accident.

Here I have summarized several issues.

1. whether or not the underlying link needs to be reused, but semantic consistency must be ensured. For a request, since it was created using URL: ftp: // ip, so it should be the root of the FTP operation rather than other directories. If this cannot be ensured, who can trust such a tool? Not to mention how it works in a multi-threaded environment.

2. For the sake of security, Most FTP will lock the user to the main directory. How can an ftpclient depend on the setting of the server? No matter who makes this mental disability setting, it is wrong.

3. When keepalive is disabled, an error is reported during the same process.

Where is the problem? I will analyze it later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.