FtpWebRequest ConnectionGroupName练习题

我对request.ConnectionGroupName有疑问。 我知道我应该为MaxservicepointIdleTime使用相同的HOST URL相同的GroupnName是100秒,在那里所有请求都将被回收。

我现在想知道的是,如果我在下面显示我的意思,对SAME主机使用不同的request.Method来使用不同的组名是好还是不好的做法?

request.Method = WebRequestMethods.Ftp.UploadFile; request.ConnectionGroupName = "Group1"; request.Method = WebRequestMethods.Ftp.DownloadFile; request.ConnectionGroupName = "Group2"; request.Method = WebRequestMethods.Ftp.DeleteFile; request.ConnectionGroupName = "Group3";

完整代码示例: Ftp.UploadFile;

void uploadimage()
        {
            String sourceimage = "C:/ESD/image_2.jpg";
            Task<bool> task = FtpUploadFile(sourceimage);
            if (task.IsFaulted == false)
            {
                MessageBox.Show(task.Result.ToString());
            }
        }
        private Task closeRequestStreamAsync(Stream requestStream) { return Task.Run(() => { requestStream.Close(); }); }
        public async Task<bool> FtpUploadFile(string filename)
        {
            //if exception occurs we want to be able to close these
            Ftpwebresponse response = null;
            FtpWebRequest request = null;
            FileStream sourceStream = null;
            Stream requestStream = null;
            try
            {
                bool isimage = false; String ext = Path.GetExtension(filename);
                if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif" || ext == ".bmp") { isimage = true; }

                request = (FtpWebRequest)WebRequest.Create("ftp://someurl.com/Folder1/test1.jpg");
                request.UsePassive = true;
                if (isimage == true) { request.UseBinary = true; } //for images
                if (isimage == false) { request.UseBinary = false; } //for text
                request.KeepAlive = true; //keep the connection open
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.ConnectionGroupName = "Group1";
                request.servicepoint.ConnectionLimit = 4;

                //These are the credentials.
                request.Credentials = new NetworkCredential("username","password");


                sourceStream = File.OpenRead(filename);
                byte[] buffer = new byte[sourceStream.Length];
                await sourceStream.ReadAsync(buffer,buffer.Length);
                sourceStream.Close();

                requestStream = await request.GetRequestStreamAsync();
                await requestStream.WriteAsync(buffer,buffer.Length);
                //MPM  This is the call that takes the time     
                await closeRequestStreamAsync(requestStream);

                //response = (Ftpwebresponse)request.GetResponse();
                webresponse responseWeb = await request.GetResponseAsync();
                response = (Ftpwebresponse)responseWeb;
                if (response.StatusDescription.Contains("226"))
                {
                    //This means that we successfully have uploaded the file!
                }
                response.Close();
                return true;
            }
            catch (Exception ex)
            {
                string errMSG = string.Format("Upload File failed,exception: {0}",ex.Message);
                return false;
            }
        }

wujianlin1984 回答:FtpWebRequest ConnectionGroupName练习题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3160589.html

大家都在问