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 ...
随机推荐
- Spring Cloud Alibaba 实战(十三) - Sleuth调用链监控
本文概要:大白话剖析调用链监控原理,然后学习Sleuth,Zipkin,然后将Sleuth整合Zipkin,最后学习Zipkin数据持久化(Elasticsearch)以及Zipkin依赖关系图 实战 ...
- Spring 框架基础(01):核心组件总结,基础环境搭建
本文源码:GitHub·点这里 || GitEE·点这里 一.Spring框架 1.框架简介 Spring是一个开源框架,框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 ...
- IO流与装饰者模式
java使用IO流来处理不同设备之间数据的交互;所有的IO操作实际上都是对 Stream 的操作 从功能上划分: 输入流: 当数据从源进入的编写的程序时,称它为输入流; 输出流: 从程序输出回另一个源 ...
- css样式优先级计算规则
css样式的优先级分为引入优先级和声明优先级. 引入优先级 引入样式一般分为外部样式,内部样式,内联样式. 外部样式:使用link引入的外部css文件. 内部样式:使用style标签书写的css样式. ...
- 多进程操作-进程锁multiprocess.Lock的使用
多进程操作-进程锁multiprocess.Lock的使用 通过之前的Process模块的学习,我们实现了并发编程,虽然更加充分地利用了IO资源,但是也有缺陷:当多个进程共用一份数据资源的时候,就 ...
- navicat 12激活
激活软件:https://github.com/DoubleLabyrinth/navicat-keygen/releases 激活说明:https://github.com/DoubleLabyri ...
- 使用laravel-amdin调用文件上传阿里oss注意点
开发者工作中,项目代码开发提高效率,往往会使用一些github上面的一些扩展类,这里举例说明一下遇到的情况. 一.使用laravel-admin框架开发管理后台文件或者图片上传 情景:运营或者产品通过 ...
- maven仓库之第二篇
1. 什么是maven? 它是一个软件开发的管理工具,主要管理的工作是:依赖管理,项目构建. 2. 使用maven的好处? 能够集中管理jar包,提供一键构建. 3. maven的安装及配置 配置:M ...
- 【Gradle】Groovy基础
Groovy基础 Groovy是基于JVM虚拟机的一种动态语言.每个Gradle的build脚本文件都是一个Groovy脚本文件. 字符串 在Groovy中,分号不是必需的.在Groovy中,单引号和 ...
- DevOps VS 职责分离
原文地址: https://medium.com/@jeehad.jebeile/devops-and-segregation-of-duties-9c1a1bea022e 原文作者:Jeehad J ...