C# WebClient,HttpClient,WebRequest
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的更多相关文章
- 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)
一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...
- WebClient, HttpClient, HttpWebRequest ,RestSharp之间的区别与抉择
NETCore提供了三种不同类型用于生产的REST API: HttpWebRequest;WebClient;HttpClient,开源社区创建了另一个名为RestSharp的库.如此多的http库 ...
- Csharp:WebClient and WebRequest use http download file
//Csharp:WebClient and WebRequest use http download file //20140318 塗聚文收錄 string filePath = "20 ...
- WebRequest/HttpWebRequest/HttpRequest/WebClient/HttpClient的区别
1.WebRequest和HttpWebRequest WebRequest 的命名空间是: System.Net ,它是HttpWebRequest的抽象父类(还有其他子类如FileWebReque ...
- webrequest HttpWebRequest webclient/HttpClient
webrequest(abstract类,不可直接用) <--- (继承)---- HttpWebRequest(更好的控制请求) <--- (继承)---- webclient (简单快 ...
- c#利用WebClient和WebRequest获取网页源代码的比较
前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...
- WebClient与WebRequest差异
WebRequst的使用 WebClient和HttpWebRequst是用来获取数据的2种方式,在我的这篇数据访问(2)中主要是讲的WebClient的使用,一般而言,WebClient更倾向于“按 ...
- C#、.NET网络请求总结(WebClient和WebRequest)
1.关于WebClient第三方的封装,支持多文件上传等 using System; using System.Collections.Generic; using System.Text; usin ...
- c#利用WebClient和WebRequest获取网页源代码
C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取网页源代码 WebClient类 WebClient ...
随机推荐
- 推荐几本高质量的Python书籍--附github下载路径
一 为什么要分享? 最近碰到了一些人和事,感触挺大的.就是发现很多类似自己的软件工程师,一旦工作三五年之后,工作中算是一个熟练工,但是进步的脚步突然慢了下来,虽然你在工作中仍旧很努力.到底是什么原因呢 ...
- SQLServer之数据库表转化为实体类【带注释】
1.在开发过程中,有时候需要将数据库表转化为实体类.手敲除了不方便,还容易出错.本着DRY+懒人原则,参考了一位老司机的博客[见底部],并在其基础上进行了优化.[原先是不带注释的] DECLARE @ ...
- MySQL数据以全量和增量方式,同步到ES搜索引擎
本文源码:GitHub·点这里 || GitEE·点这里 一.配置详解 场景描述:MySQL数据表以全量和增量的方式向ElasticSearch搜索引擎同步. 1.下载内容 elasticsearch ...
- GROUP BY中的WITH CUBE、WITH ROLLUP原理测试及GROUPING应用
前几天,看到一个群友用WITH ROLLUP运算符.由于自个儿没用过,看到概念及结果都云里雾里的,所以突然来了兴趣对生成结果测了一番. 一.概念: WITH CUBE:生成的结果集显示了所选列中值的所 ...
- 在Windows下配置多个git账号
1.生成并部署SSH key 安装好Git客户端后,打开git bash,输入以下命令生成user1的SSH Key: ssh-keygen -t rsa -C "user1@email.c ...
- JS For
JS For 循环可以将代码块执行指定的次数. JavaScript 循环 document.write(cars[0] + "<br>"); document.wri ...
- JS 错误
JS 错误 try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. 错误一定会发生 当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错 ...
- opencv-python 图像基础处理(四)
图像梯度-Sobel算子 Gx等于左边减去右边 Gy等于下减去上 可以得到像素值 dst = cv2.Sobel(src, ddepth, dx, dy, ksize)- ddepth:图像的深 ...
- Fiddler应用——Fiddler过滤功能
Fiddler的过滤功能在Fiddler右面板处,点击Filters显示如图所示面板. 如图所示,Fiddler的过滤面板主要分为几个部分: 1.Use Filters:是否启用过滤器 2.Actio ...
- python使用beautifulsoup4爬取酷狗音乐
声明:本文仅为技术交流,请勿用于它处. 小编经常在网上听一些音乐但是有一些网站好多音乐都是付费下载的正好我会点爬虫技术,空闲时间写了一份,截止4月底没有问题的,会下载到当前目录,只要按照bs4库就好, ...