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-paramiko对远程服务器终端的操作

    1.with open写文件到本地 2.paramiko SFTPClient将文件推到salt服务端 3.paramiko SSHClient通过salt-cp将文件分发给目标服务器 1. with ...

  2. node http 模块 常用知识点记录

    关于 node,总是断断续续的学一点,也只能在本地自己模拟实战,相信总会有实战的一天~~ http 作为服务端,开启服务,处理路由,响应等 http 作为客户端,发送请求 http.https.htt ...

  3. (五十九)c#Winform自定义控件-池子(工业)-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  4. 【ASP.NET Core学习】基础

    新建项目时,程序入口调用CreateDefaultBuilder(args),下面是源代码 public static IHostBuilder CreateDefaultBuilder(string ...

  5. 一种简单的REST API接口加密实现,只允许自己的产品调用后台,防止接口被刷

    在项目上线后,后台接口很容易通过抓包工具看到, 难免被人为构造恶意请求攻击我们的系统,相信大家都或多或少都遇到过短信验证码被刷.疯狂留言灌水.数据被恶意爬取等问题,这种直接抓接口然后写个循环调用的行为 ...

  6. Mac进行Flutter的相关开发配置

    参考链接:https://www.cnblogs.com/tangtianming/p/10797227.html

  7. ubuntu 18.04下安装JDK

    一.安装前检查 检查是否已经安装 java -version 二.安装方式 1)通过ppa(源) 2)通过官网安装包安装  JDK官网下载地址  或百度云下载地址,提取码 rzq5 三.安装步骤 (一 ...

  8. C Primer Plus 第六版—— 6.16 编程练习题(附代码)

    1.编写一个程序,创建一个包含26个元素的数组,并在其中存储26个小写字母.然后打印数组的所有内容. #include <stdio.h> int main(void) { int num ...

  9. Putty 连接centOS7 超时问题

    方法1: #vim /etc/ssh/sshd_config(添加或修改以下配置) ClientAliveInterval 60(每隔60秒给SSH客户端发送一次信号)   ClientAliveCo ...

  10. JVM 类的加载机制

    在对类的实例化之前.JVM 一般会先进行初始化 主要经过如下几个阶段: 1.加载                       类加载的第一阶段,类加载时机有两个: 1.预加载:当虚拟机启动时,会预加载 ...