The underlying connection was closed: the server committed a Protocol Violation

Source: Internet
Author: User

I. Problem generation:

An inexplicable error occurred while downloading files from the FTP server using C. (The test is normal on the local machine. This occurs only when deployed on the server, and the error prompt is strange ).

"The underlying connection was closed: the server committed a protocol violation" is translated into Chinese: "The basic connection has been closed: the server has submitted the Protocol"

The program code is as follows:

Code

Ftpaddress = "ftp: // 192.168.1.1/0409/1234 *****. jpg ";
Uri serveruri = new uri (ftpaddress );

WebClient listrequest = new WebClient ();
Listrequest. Credentials = new networkcredential ("user", "PWD ");
Try
{
Byte [] newimagefile = listrequest. downloaddata (serveruri );
Return generateimgsrc (newimagefile );
}
Catch (exception ex)
{
Throw ex;
}

The above code snippet prompts that the local execution is normal. When I trace the server, I find that the exception is thrown by the listrequest. downloaddata () method.

Ii. cause:

After multiple tests on the deployed server, downloaddata reports an error because invalid characters are returned during File Download through FTP.

But why is there no problem on the local machine? I developed this machine in the. NET Framework 2.0 Environment. The. NET Framework 2.0 is also deployed on the server. Is the difference in the environment changing?

I thought about whether other frameworks on my local machine have affected the normal operation of the local machine. on the server, only. NET Framework 2.0 does not work properly.

Suddenly I thought about the version. Will the. NET framewrok 2.0 version be different? The WebClient. downloaddata method is used in the namespace of the system. dll assembly. Open the c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ System. dll Directory. The server version 2.0.50727.42 is found, and the local version of my server is 2.0.50727.4016, and the file size is also different.

Finally, I found the reason why the server cannot run normally? Although both are. NET Framework 2.0, the versions on the local machine and server are different.

Iii. solution:

With. NET Framework 2.0 SP1, the deployed server can run normally.

After installing. NET Framework 2.0 SP1 on the server, we found that the version of system. dll has changed to 2.0.50727.1433. Although it is different from my local version, it can be downloaded through WebClient. downloaddata!

 

Finally, the problem above is found in the project. Here is just a development note. To facilitate future search. At the same time, I also hope to provide my friends with an idea of solving problems.

(Because different software and frameworks are installed in the environment of the Local Machine and server, the running environment is different. How can we identify the differences between the two ends, this is an effective solution ).

Best regards,

Charles Chen

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.