static  void WebClientDemo()
{
string url = "https://www.cnblogs.com/Fred1987/p/11843418.html";
WebClient wc = new WebClient();
string content=wc.DownloadString(url);
Console.WriteLine(content);
} static async void HttpClientDemo()
{
try
{
string url = "https://www.cnblogs.com/Fred1987/p/11843418.html";
using (HttpClient httpClient = new HttpClient())
{
HttpResponseMessage httpResponse = await httpClient.GetAsync(url);
httpResponse.EnsureSuccessStatusCode();
string responseBody = await httpResponse.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch(HttpRequestException ex)
{
Console.WriteLine(ex.Message);
}
} static void WebRequestDemo()
{
string url = "https://www.cnblogs.com/Fred1987/p/11843418.html";
WebRequest webRequest = WebRequest.Create(url);
using (WebResponse webResponse = webRequest.GetResponse())
{
Stream responseStream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
string result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
}

static void Main(string[] args)
{
WebRequestAsyncDemo();
Console.ReadLine();
} static void WebRequestAsyncDemo()
{
string url = "https://docs.microsoft.com/en-us/dotnet/api/system.string?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev16.query%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.String);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.8);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.8"; Task task = WriteWebRequestAsync(url);
try
{
while(!task.Wait())
{
Console.Write(".");
}
}
catch(AggregateException aes)
{
aes = aes.Flatten();
try
{
aes.Handle(x =>
{
Console.WriteLine(x.Message);
return true;
});
}
catch(WebException)
{ }
catch(IOException)
{ }
catch(NotSupportedException)
{ }
}
} static Task WriteWebRequestAsync(string url)
{
StreamReader reader = null;
WebRequest request = WebRequest.Create(url);
Task task = request.GetResponseAsync().ContinueWith(x =>
{
WebResponse response = x.Result;
reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEndAsync();
}).Unwrap().ContinueWith(x =>
{
if (reader != null)
{
reader.Dispose();
}
string text = x.Result;
Console.WriteLine(text);
});
return task;
}

static void Main(string[] args)
{
WebRequestDemoAsync();
Console.ReadLine();
}

static async void WebRequestDemoAsync()
{
string url = "https://docs.microsoft.com/en-us/dotnet/api/system.string?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev16.query%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.String);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.8);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.8";
WebRequest request = WebRequest.Create(url);
WebResponse response =await request.GetResponseAsync();
using(StreamReader reader=new StreamReader(response.GetResponseStream()))
{
string text = await reader.ReadToEndAsync();
Console.WriteLine(text);
}
}

C# WebClient,HttpClient,WebRequest的更多相关文章

  1. 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)

    一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...

  2. WebClient, HttpClient, HttpWebRequest ,RestSharp之间的区别与抉择

    NETCore提供了三种不同类型用于生产的REST API: HttpWebRequest;WebClient;HttpClient,开源社区创建了另一个名为RestSharp的库.如此多的http库 ...

  3. Csharp:WebClient and WebRequest use http download file

    //Csharp:WebClient and WebRequest use http download file //20140318 塗聚文收錄 string filePath = "20 ...

  4. WebRequest/HttpWebRequest/HttpRequest/WebClient/HttpClient的区别

    1.WebRequest和HttpWebRequest WebRequest 的命名空间是: System.Net ,它是HttpWebRequest的抽象父类(还有其他子类如FileWebReque ...

  5. webrequest HttpWebRequest webclient/HttpClient

    webrequest(abstract类,不可直接用) <--- (继承)---- HttpWebRequest(更好的控制请求) <--- (继承)---- webclient (简单快 ...

  6. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

  7. WebClient与WebRequest差异

    WebRequst的使用 WebClient和HttpWebRequst是用来获取数据的2种方式,在我的这篇数据访问(2)中主要是讲的WebClient的使用,一般而言,WebClient更倾向于“按 ...

  8. C#、.NET网络请求总结(WebClient和WebRequest)

    1.关于WebClient第三方的封装,支持多文件上传等 using System; using System.Collections.Generic; using System.Text; usin ...

  9. c#利用WebClient和WebRequest获取网页源代码

    C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现.   WebClient类获取网页源代码   WebClient类   WebClient ...

随机推荐

  1. 利用Python进行数据分析-Pandas(第六部分-数据聚合与分组运算)

    对数据集进行分组并对各组应用一个函数(无论是聚合还是转换),通常是数据分析工作中的重要环节.在将数据集加载.融合.准备好之后,通常是计算分组统计或生成透视表.pandas提供了一个灵活高效的group ...

  2. Java题库——Chapter12 异常处理和文本IO

    异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...

  3. 创建多进程之multiprocess包中的process模块

    创建多进程之multiprocess包中的process模块 1.process模块是一个创建进程的模块 Process([group [, target [, name [, args [, kwa ...

  4. Redis在Window下的安装部署

    一.下载 由于redis官方不支持windows,所以需要在github上下载window的版本:下载地址.redis约定版次版本号(即第一个小数点后的数字)为偶数的版本是稳定版本(如2.8,3.0) ...

  5. 3D开发基础知识和简单示例

    引言 现在物联网概念这么火,如果监控的信息能够实时在手机的客服端中以3D形式展示给我们,那种体验大家可以发挥自己的想象. 那生活中我们还有很多地方用到这些,如上图所示的Kinect 在医疗上的应用,当 ...

  6. Swoole中内置Http服务器

    创建httpServer.php文件,代码如下: <?php // 创建服务对象 $http = new swoole_http_server("10.211.55.17", ...

  7. 剑指offer 22:验证栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...

  8. SQL Server如何查看存储过程的执行计划

    有时候,我们需要查看存储过程的执行计划,那么我们有什么方式获取存储过程的历史执行计划或当前的执行计划呢? 下面总结一下获取存储过程的执行计划的方法. 1:我们可以通过下面脚本查看存储过程的执行计划,但 ...

  9. LEETCODE 1254 统计封闭岛屿的数目 Number of Closed Islands

    地址 https://leetcode-cn.com/contest/weekly-contest-162/problems/number-of-closed-islands/ 有一个二维矩阵 gri ...

  10. 04. Go 语言流程控制

    Go 语言流程控制 流程控制是每种编程语言控制逻辑走向和执行次序的重要部分,流程控制可以说是一门语言的"经脉". Go 语言的常用流程控制有 if 和 for,而 switch 和 ...