How to solve the problem that the basic ftpwebrequest connection has been closed; An error occurred while receiving (zz)

Source: Internet
Author: User

On the FTP server, I added some Chinese welcome words to the "messages" tab.
BelowCodeAfter debugging, the system prompts "the basic connection has been closed; An error occurred while receiving". After querying relevant information, I can find the following methods to solve this problem.

Method: remove the Chinese welcome word from the FTP server or change it to an English welcome word. This is a Microsoft Bug.

Code:


Public void upload (string filename)
{

// ip
string ftpip = system. web. configuration. webconfigurationmanager. deleetpipeline ["ftpip"];
// user name
string ftpusername = system. web. configuration. webconfigurationmanager. appsettings ["ftpusrname"];
// User Password
string ftppassord = system. web. configuration. webconfigurationmanager. deleetask[ "ftpusrpsw"];
fileinfo fileinf = new fileinfo (filename);
string uri = "ftp: // "+ ftpip +"/sys/"+ fileinf. name;
ftpwebrequest reqftp;

// Create an ftpwebrequest object based on the URI
Reqftp = (ftpwebrequest) ftpwebrequest. Create (New uri (URI ));

// FTP user name and password
Reqftp. Credentials = new networkcredential (ftpusername, ftppassord );

// The default value is true, and the connection will not be closed
// Executed after a command
Reqftp. keepalive = false;

// Specify the command to be executed
Reqftp. method = webrequestmethods. FTP. uploadfile;

// Specify the data transmission type
Reqftp. usebinary = true;

// Notify the server of the file size when uploading the file
Reqftp. contentlength = fileinf. length;

// Set the buffer size to 2 kb.
Int bufflength = 2048;

Byte [] buff = new byte [bufflength];
Int contentlen;

// open a file stream (system. io.
filestream FS = fileinf. openread ();
try
{< br> // write the uploaded file to the stream
// stream STRM = reqftp. getrequeststream ();
stream STRM = reqftp. getrequeststream ();
// 2kb of each file stream read
contentlen = FS. read (buff, 0, bufflength);

// The stream content has not ended
While (contentlen! = 0)
{
// Write 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 ();
}< br> catch (exception ex)
{< br> throw ex;
}< BR >}

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.