C# NutShell 第十六章 网络

地址与端口

            var a1 = new IPAddress(new byte[] { 101,102,103,104});
            var a2 = IPAddress.Parse("101.102.103.255");
            var p1 = new IPEndPoint(a1, 200);

URI

1.URI分为3部分,协议(schema),权限(authority),路径(path)

2.Uri类将地址看成字符串,并且将地址按照协议分开成3部分

3.文件路径也可以看成是URI(前面加上File:)

            Uri info = new Uri("http://www.baodi.com/info/");
            Console.WriteLine(info.AbsolutePath);
            Console.WriteLine(info.Host);

4.URI后面的斜杠非常重要。如果最后又斜杠,则HTTP服务器会在Web文件夹中查找info文件夹,然后返回该文件夹的默认文档

而如果没有斜杠的话,则会直接查找该文件(通常会忽略扩展名),所以不应该。

客户端类型

1.WebRequest和WebResponse是管理HTTP和FTP客户端的

2.WebClient集成了WebRequest和WebResponse的功能,更加方便了

3.HttpClient也集成了WebRequest和WebResponse的功能,针对HTTP

WebClient类

1.使用步骤

实例化WebClient对象

这是Proxy属性

如果需要验证,设置Credentials属性值

使用相应的URI调用Down或Upload方法

2.例子

WebClient wc = new WebClient();
wc.DownloadFile("http://www.albahari.com/nutshell/code.aspx", "code.html");
System.Diagnostics.Process.Start("code.html");

3.异步调用,进度报告

            WebClient wc = new WebClient();
            wc.DownloadProgressChanged += (sender, arg) =>
            {
                Console.WriteLine(arg.ProgressPercentage + "% complete");
            };
            await Task.Delay(5000).ContinueWith(ant => wc.CancelAsync());
            await wc.DownloadFileTaskAsync("https://www.oreilly.com/", "webpage.htm");

进度报告DownloadProgressChanged会获取当前下载进度。

同一个WebClient对象避免进行多个进度报告功能或者取消功能

4.WebClient对象虽然继承了IDispose对象,但是实际上并没有执行什么操作,所以可以不使用using

WebRequest和WebResponse

1.WebRequest和WebResponse比WebClient更加复杂,但是也更加灵活

2.例子

            WebRequest req = WebRequest.Create("http://www.albahari.com/nutshell/code.html");
            req.Proxy = null;
            using (WebResponse res = req.GetResponse())
            {
                using (Stream rs = res.GetResponseStream())
                {
                    using (FileStream fs = File.Create("code.html"))
                    {
                        rs.CopyTo(fs);
                    }
                }
            }

3.异步例子

        static async void Fun()
        {
            WebRequest req = WebRequest.Create("http://www.albahari.com/nutshell/code.html");
            req.Proxy = null;
            using (WebResponse res = await req.GetResponseAsync())
            {
                using (Stream rs = res.GetResponseStream())
                {
                    using (FileStream fs = File.Create("code1.html"))
                    {
                        await rs.CopyToAsync(fs);
                    }
                }
            }
        }

4.一个WebRequest不可用于多个请求

HttpClient

1.在HttpWebRequest和HttpWebResponse上提供了一层封装

2.一个HttpClient实例可以支持并发请求

3.HttpClient支持插件式的自定义消息处理器

4.HttpCLient有丰富且易于扩展的请求头部与内容类型系统

5.HttpClient不能完全替代WebClient。HttpClient不支持进度报告。不支持FTP,file协议,自定义URI模式

GetAsync方法与相应消息

            var client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync("https://www.feiniaomuyu.t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值