Home > other >  C # FTP download asynchronous problem
C # FTP download asynchronous problem

Time:10-06

Code:
Private void the Download ()
{
Uri the Uri=new Uri (" ftp://xx.xx.xx.xx/hello.txt ");


FtpWebRequest downloadRequest=(FtpWebRequest) WebRequest. Create (uri).
DownloadRequest. Method=WebRequestMethods. Ftp. DownloadFile;

String ftpUser="x" x ";
String ftpPassWord="x 'x' x 'x' x 'x' x";
DownloadRequest. Credentials=new NetworkCredential (ftpUser, ftpPassWord);


DownloadRequest. BeginGetRequestStream (new AsyncCallback (EndGetStreamCallback2), downloadRequest);
}
Private void EndGetStreamCallback2 IAsyncResult (ar)
{
The Debug Log (" -=-=-=");

FtpWebRequest downloadRequest=(FtpWebRequest) ar. AsyncState;


The Stream requestStream=downloadRequest. EndGetRequestStream (ar);

FileStream outputStream=new FileStream (" "+" \ \ "+" hello. TXT ", FileMode. Create);
Byte [] buffer=new byte [2048].
Int bytesRead;
While (true)
{
BytesRead=outputStream. Read (buffer, 0, buffer Length);
If (bytesRead==0)
break;


RequestStream. Write (buffer, 0, bytesRead);
}

RequestStream. Close ();
OutputStream. Close ();


DownloadRequest. BeginGetResponse (new AsyncCallback (EndGetResponseCallback2), downloadRequest);
}


Private void EndGetResponseCallback2 IAsyncResult (ar)
{
FtpWebRequest downloadRequest=(FtpWebRequest) ar. AsyncState;


FtpWebResponse downloadResponse=(FtpWebResponse) downloadRequest. EndGetResponse (ar);

The Debug Log (downloadResponse. StatusDescription);
The Debug Log (" download complete ");
}

Refer to the great god! How to modify, please!
  • Related