C#WebClient崩溃我的应用程序!

时间:2010-09-22 07:22:35

标签: c# .net download webclient

我正在尝试运行此代码:

remoteProducts.ForEach(delegate(RemoteProduct p)
                {
                    this.toolStripStatus.Text = "Downloading product: " + this.progress.Value + "/" + remoteProducts.Count;

                    try
                    {
                        prodLocalDao.addProduct(p.Id, p.Category, p.SubCategory, p.Sku);
                        int localProductId = p.Id;

                        List<ProductAttribute> attributes = prodRemoteDao.getProductAttributesByProductId(p.Id);
                        for (int i = 0; i < attributes.Count; i++)
                        {
                            prodLocalDao.addAttribute(localProductId, attributes[i].Id, attributes[i].Name, attributes[i].Value);
                        }

                        // Add photos
                        ArrayList photos = prodRemoteDao.getPhotosFromRemoteByProductId(p.Id);
                        DirectoryInfo directory = new DirectoryInfo(String.Format("photos\\{0}\\", p.Id));
                        if (!directory.Exists) directory.Create();

                        for (int i = 0; i < photos.Count; i++)
                        {
                            FileInfo file = new FileInfo(photos[i].ToString());
                            this.DownloadFTP(photos[i].ToString(), directory.FullName + file.Name);
                        }

                        this.toolStripStatus.Text += "... Done";
                    }
                    catch
                    {
                        this.toolStripStatus.Text += "... Skiped";
                    }


                    this.progress.Value++;
                    this.Refresh();
                });

它会正确下载第一个产品的图像,但是当它尝试为第二个产品运行此行时:

prodLocalDao.addProduct(p.Id, p.Category, p.SubCategory, p.Sku);

它崩溃了我的应用程序,说vhost已停止工作,这些是它显示的详细信息:

  

问题签名:问题事件   名称:APPCRASH应用程序   名称:桌面管理器.vshost.exe
  应用版本:9.0.21022.8
  申请时间戳:47316898
  故障模块名称:winhttp.dll故障   模块版本:6.1.7600.16385故障   模块时间戳:4a5bdb3e异常   代码:c0000005例外   偏移量:000015cb OS   版本:6.1.7600.2.0.0.256.1区域设置   ID:1033附加信息   1:0a9e附加信息   2:0a9e372d3b4ad19135b953a78882e789
  附加信息3:0a9e
  附加信息   4:0a9e372d3b4ad19135b953a78882e789

     

在线阅读我们的隐私声明:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

     

如果没有在线隐私声明   可用,请阅读我们的隐私   离线声明:
  C:\ WINDOWS \ SYSTEM32 \ EN-US \ erofflps.txt

DownloadFtp功能

public void DownloadFTP(string RemotePath, string LocalPath)
        {
            BasicFTPClient MyClient = new BasicFTPClient();
            MyClient.Host = Desktop_Manager.Properties.Settings.Default["ftphost"].ToString();
            MyClient.Username = Desktop_Manager.Properties.Settings.Default["ftpuser"].ToString();
            MyClient.Password = Desktop_Manager.Properties.Settings.Default["ftppass"].ToString();


            string remotePath = String.Format("/media/catalog/product{0}", RemotePath);
            MyClient.DownloadFile(remotePath, LocalPath);
        }

现在是BasicFTPClient类

using System;
using System.Net;
using System.IO;

namespace BasicFTPClientNamespace
{
    class BasicFTPClient
    {
        public string Username;
        public string Password;
        public string Host;
        public int Port;

        public BasicFTPClient()
        {
            Username = "anonymous";
            Password = "anonymous@internet.com";
            Port = 21;
            Host = "";
        }

        public BasicFTPClient(string theUser, string thePassword, string theHost)
        {
            Username = theUser;
            Password = thePassword;
            Host = theHost;
            Port = 21;
        }

        private Uri BuildServerUri(string Path)
        {
            return new Uri(String.Format("ftp://{0}:{1}/{2}", Host, Port, Path));
        }

        /// <summary>
        /// This method downloads the given file name from the FTP server
        /// and returns a byte array containing its contents.
        /// Throws a WebException on encountering a network error.
        /// </summary>

        public byte[] DownloadData(string path)
        {
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // Logon to the server using username + password
            request.Credentials = new NetworkCredential(Username, Password);
            return request.DownloadData(BuildServerUri(path));
        }

        /// <summary>
        /// This method downloads the FTP file specified by "ftppath" and saves
        /// it to "destfile".
        /// Throws a WebException on encountering a network error.
        /// </summary>
        public void DownloadFile(string ftppath, string destfile)
        {
            // Download the data
            byte[] Data = DownloadData(ftppath);

            // Save the data to disk
            FileStream fs = new FileStream(destfile, FileMode.Create);
            fs.Write(Data, 0, Data.Length);
            fs.Close();
        }

        /// <summary>
        /// Upload a byte[] to the FTP server
        /// </summary>
        /// <param name="path">Path on the FTP server (upload/myfile.txt)</param>
        /// <param name="Data">A byte[] containing the data to upload</param>
        /// <returns>The server response in a byte[]</returns>

        public byte[] UploadData(string path, byte[] Data)
        {
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // Logon to the server using username + password
            request.Credentials = new NetworkCredential(Username, Password);
            return request.UploadData(BuildServerUri(path), Data);
        }

        /// <summary>
        /// Load a file from disk and upload it to the FTP server
        /// </summary>
        /// <param name="ftppath">Path on the FTP server (/upload/myfile.txt)</param>
        /// <param name="srcfile">File on the local harddisk to upload</param>
        /// <returns>The server response in a byte[]</returns>

        public byte[] UploadFile(string ftppath, string srcfile)
        {
            // Read the data from disk
            FileStream fs = new FileStream(srcfile, FileMode.Open);
            byte[] FileData = new byte[fs.Length];

            int numBytesToRead = (int)fs.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
            {
                // Read may return anything from 0 to numBytesToRead.
                int n = fs.Read(FileData, numBytesRead, numBytesToRead);

                // Break when the end of the file is reached.
                if (n == 0) break;

                numBytesRead += n;
                numBytesToRead -= n;
            }
            numBytesToRead = FileData.Length;
            fs.Close();

            // Upload the data from the buffer
            return UploadData(ftppath, FileData);
        }

    }
}

任何想法为什么?? !!

感谢...

0 个答案:

没有答案