zoukankan      html  css  js  c++  java
  • C#利用WebClient 两种方式下载文件

    WebClient client = new WebClient();

    第一种

    string URLAddress = @"http://files.cnblogs.com/x4646/tree.zip";

    string receivePath=@"C:";

    client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));

    就OK了。

    第二种

     Stream str = client.OpenRead(URLAddress);
       StreamReader reader = new StreamReader(str);
       byte[] mbyte = new byte[1000000];
       int allmybyte = (int)mbyte.Length;
       int startmbyte = 0;

       while (allmybyte > 0)
       {

        int m = str.Read(mbyte, startmbyte, allmybyte);
        if (m == 0)
         break;

        startmbyte += m;
        allmybyte -= m;
       }

       reader.Dispose();
       str.Dispose();

       string path = receivePath + System.IO.Path.GetFileName(URLAddress);
       FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
       fstr.Write(mbyte, 0, startmbyte);
       fstr.Flush();
       fstr.Close();

    利用webclient读取html内容

    public static string GetWebClient(string url)

    {
        string strHTML = "";
        WebClient myWebClient = new WebClient();            
        Stream myStream = myWebClient.OpenRead(url);
        StreamReader sr = new StreamReader(myStream, Encoding.Default);//注意编码
        strHTML = sr.ReadToEnd();
        myStream.Close();
        return strHTML;
    }
  • 相关阅读:
    xen xl命令记录
    fiefox 自动下载文件配置
    ActionChains用法
    WebDriverWait
    python3 安装selenium
    mysql 不能执行delete检查safe-updates模式
    shell 自动部署替换版本和备份
    matlab 非线性拟合
    【递归】1208:2的幂次方表示
    二进制和位运算符
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/6838742.html
Copyright © 2011-2022 走看看