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 ...
随机推荐
- CSharpGL(57)[译]Vulkan清空屏幕
CSharpGL(57)[译]Vulkan清空屏幕 本文是对(http://ogldev.atspace.co.uk/www/tutorial51/tutorial51.html)的翻译,作为学习Vu ...
- Unix 开发中的 Make 三连
Unix 开发过程中,经常性的操作是从源码编译安装相应库文件,所以下面三个命令便是家常便饭,俗称三连: ./configure make make install 下面来看看这三步分别做了什么. co ...
- C# 英语纠错 LanguageTool
WPF中,对单词拼写错误,textbox有相应的附加属性可以设置. <TextBox SpellCheck.IsEnabled="True" /> 但是此属性只在WPF ...
- Specify Action Settings 指定按钮设置
In this lesson, you will learn how to modify Action properties. The ClearTasks Action will be used. ...
- 清新淡雅教育教学工作课件PPT模板
模板来源:http://ppt.dede58.com/jiaoxuekejian/26240.html
- Servlet、Jsp
一.Servlet 1.什么是Servlet? (1)由sun公司(被oracle公司收购)制定的一种用来扩展web服务器功能的组件规范.简单的讲就是一种用来开发动态Web的技术. 扩展web服务器功 ...
- jsb闭包
1.什么是闭包? anw:能够读取其他函数内部变量的函数 本质:将函数内部与函数外部连接起来 2.由于在js中,只有函数内部的子函数才能读取局部变量,因此可以把闭包简单理解成’定义一个在函数内部的函数 ...
- View和ViewGroup
1.继承关系 2.组合关系 3.View 的绘制流程 3.1.创建R.attrs.styleable,申明需要用到的属性值,在使用时可以根据属性进行定义 3.2.extends View ,依次 ...
- msyql master thread
------------------------------------------------------ 2015-02-10----------------------------------- ...
- MyBatis之接口绑定方案及多参数传递
1.说明 所谓的MyBatis接口绑定,指的是实现创建一个接口后,把mapper.xml 由mybatis 生成接口的实现类,通过调用接口对象就可以获取mapper.xml 中编写的sql.在SS ...