网络客户端,用于从已部署的 Web 下载数据

本文关键字:Web 下载 数据 部署 客户端 用于 网络 | 更新日期: 2023-09-27 18:30:17

我刚刚在IIS上部署了我的Web,在我的WPF端,我想从中下载数据,所以我是这样做的:

   public StartWindow()
    {
        InitializeComponent();
        label11.Width = Double.NaN;
        stackPanelTitle.Width = Double.NaN;
        DownloadData("http://localhost/iStellarMobile_deploy/Puzzle/CrossTest.txt"); // null exception 
    }
    protected void DownloadData(string strFileUrlToDownload)
    {
        byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception 
        MemoryStream storeStream = new MemoryStream();
        storeStream.SetLength(myDataBuffer.Length);
        storeStream.Write(myDataBuffer, 0, (int)storeStream.Length);
        storeStream.Flush();
        using (FileStream file = new FileStream(@"C:'TestFile.txt", FileMode.Create, System.IO.FileAccess.Write))
        {
            byte[] bytes = new byte[storeStream.Length];
            storeStream.Read(bytes, 0, (int)storeStream.Length);
            file.Write(bytes, 0, bytes.Length);
            storeStream.Close();
        }
        //TO save into certain file must exist on Local
       //storeStream.SaveAs(storeStream,  @"C:'TestFile.txt");
        //The below Getstring method to get data in raw format and manipulate it as per requirement
        string download = Encoding.ASCII.GetString(myDataBuffer);

    }

这是正确的方法吗? 我指的是 http://www.c-sharpcorner.com/UploadFile/97fc7a/reading-files-from-given-specific-url-using-webclient/

它在我评论的 2 行上给了我空异常.

网络客户端,用于从已部署的 Web 下载数据

为什么要使用new uri

 byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception

下面是一个直接取自 MSDN 的示例

WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);

http://msdn.microsoft.com/en-us/library/xz398a3f.aspx