求助。CS1729 '“WebClient”不包含采用 1 个参数的构造函数

weixin_46707251 2020-03-31 10:57:19
buffer = null;
System.Net.WebClient webClient = new System.Net.WebClient("https://music.douban.com/artists/genre_page/8/");
System.Net.WebClient webclient = webClient;
buffer = webclient.DownloadData("https://music.douban.com/artists/genre_page/8/");
string html = System.Text.Encoding.GetEncoding("utf-8").GetString(buffer);
...全文
423 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_aspnet 2020-04-10
  • 打赏
  • 举报
回复
public class WebClientHelper
    {
        public static string DownloadString(string url)
        {
            WebClient wc = new WebClient();
            //wc.BaseAddress = url;   //设置根目录
            wc.Encoding = Encoding.UTF8;    //设置按照何种编码访问,如果不加此行,获取到的字符串中文将是乱码
            string str = wc.DownloadString(url);
            return str;
        }
        public static string DownloadStreamString(string url)
        {
            WebClient wc = new WebClient();
            wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
            Stream objStream = wc.OpenRead(url);
            StreamReader _read = new StreamReader(objStream, Encoding.UTF8);    //新建一个读取流,用指定的编码读取,此处是utf-8
            string str = _read.ReadToEnd();
            objStream.Close();
            _read.Close();
            return str;
        }

        public static void DownloadFile(string url, string filename)
        {
            WebClient wc = new WebClient();
            wc.DownloadFile(url, filename);     //下载文件
        }

        public static void DownloadData(string url, string filename)
        {
            WebClient wc = new WebClient();
            byte [] bytes = wc.DownloadData(url);   //下载到字节数组
            FileStream fs = new FileStream(filename, FileMode.Create);
            fs.Write(bytes, 0, bytes.Length); 
            fs.Flush();
            fs.Close();
        }

        public static void DownloadFileAsync(string url, string filename)
        {
            WebClient wc = new WebClient();
            wc.DownloadFileCompleted += DownCompletedEventHandler;
            wc.DownloadFileAsync(new Uri(url), filename);
            Console.WriteLine("下载中。。。");
        }
        private static void DownCompletedEventHandler(object sender, AsyncCompletedEventArgs e)
        {
            Console.WriteLine(sender.ToString());   //触发事件的对象
            Console.WriteLine(e.UserState);
            Console.WriteLine(e.Cancelled);
            Console.WriteLine("异步下载完成!");
        }

        public static void DownloadFileAsync2(string url, string filename)
        {
            WebClient wc = new WebClient();
            wc.DownloadFileCompleted += (sender, e) =>
            {
                Console.WriteLine("下载完成!");
                Console.WriteLine(sender.ToString());
                Console.WriteLine(e.UserState);
                Console.WriteLine(e.Cancelled);
            };
            wc.DownloadFileAsync(new Uri(url), filename);
            Console.WriteLine("下载中。。。");
        }
    }
杀马特丶蛮牛 2020-04-01
  • 打赏
  • 举报
回复
webclient只有无参构造函数,你放参数进去干嘛?你百度搜一下.net webclient一抓一大把例子
usecf 2020-04-01
  • 打赏
  • 举报
回复
“WebClient”不包含采用 1 个参数的构造函数 不是提示你了么 看看你参数个数 找你对应的这个方法 看看需要几个参数

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧