第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)
一. 前言
前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍; 后端调用大约有这些:WebCient、WebRequest、Httpclient、WebapiClient,重点探讨Get和Post请求,Put和Delete请求用较少。
下面排着介绍Get和Post的基本写法,最后再封装一下,便于调用。
PS:其中Post请求有两种,分别是: "application/x-www-form-urlencoded"表单提交的方式 和 "application/json" Json格式提交的方式。
(1). Post的表单提交的格式为:"userName=admin&pwd=123456"。
(2). Post的Json的提交格式为:将实体(类)转换成json字符串。
下面先编写几个服务器端的接口方法,便于调用。
//Get http://localhost:2131/api/Forth/CheckLogin?userName=admin&pwd=123456
[HttpGet]
public string CheckLogin(string userName, string pwd)
{
if (userName == "admin" && pwd == "")
{
return "ok";
}
else
{
return "error";
}
} //Post http://localhost:2131/api/Forth/Register
[HttpPost]
public string Register([FromBody]LoginModel model)
{
if (model.userName == "admin" && model.pwd == "")
{
return "ok";
}
else
{
return "error";
}
} //Post http://localhost:2131/api/Forth/Register2
[HttpPost]
public string Register2([FromBody]dynamic model)
{
if (model.userName == "admin" && model.pwd == "")
{
return "ok";
}
else
{
return "error";
}
}
为了方便下面的测试,给上述三个地址进行命名描述和序列化方法的初始化
二. WebClient
1. Get请求
WebClient wc = new WebClient();
string url = url1;
wc.Encoding = Encoding.UTF8;
string result = wc.DownloadString(url);
Console.WriteLine(result);
Console.ReadKey();
2. Post的表单提交方式(url2能访问,url3报500错误)
WebClient wc = new WebClient();
string url = url3;
wc.Encoding = Encoding.UTF8;
//也可以向表头中添加一些其他东西
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string result = wc.UploadString(url, "userName=admin&pwd=123456");
Console.WriteLine(result);
Console.ReadKey();
3. Post的JSON提交格式(url2和url3都能正常访问)
var user = new
{
userName = "admin",
pwd = ""
};
WebClient wc = new WebClient();
string url = url3;
wc.Encoding = Encoding.UTF8;
wc.Headers.Add("Content-Type", "application/json");
10 string result = wc.UploadString(url, jss.Serialize(user));
Console.WriteLine(result);
Console.ReadKey();
三. WebRequest
1. Get请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
request.Timeout = * ;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
Console.WriteLine(result);
Console.ReadKey();
2. Post的表单提交方式(url2能访问,url3报500错误)
var postData = "userName=admin&pwd=123456";
var request = HttpWebRequest.Create(url2) as HttpWebRequest;
request.Timeout = * ;//设置30s的超时
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data, , data.Length);
postStream.Close();
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
Console.WriteLine(result);
Console.ReadKey();
3. Post的JSON提交格式(url2和url3都能正常访问)
var user = new
{
userName = "admin",
pwd = ""
};
var postData = jss.Serialize(user);
var request = HttpWebRequest.Create(url2) as HttpWebRequest;
request.Timeout = * ; //设置30s的超时
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/json";
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data, , data.Length);
postStream.Close();
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
Console.WriteLine(result);
Console.ReadKey();
四. HttpClient
1. Get请求
var handler = new HttpClientHandler();
using (var http = new HttpClient(handler))
{
var response = http.GetAsync(url1).Result;
//获取Http的状态值
//Console.WriteLine(response.StatusCode);
string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
Console.ReadKey();
}
2. Post的表单提交方式(url2能访问,url3报500错误)
var handler = new HttpClientHandler();
using (var http = new HttpClient(handler))
{
var content = new StringContent("userName=admin&pwd=123456", Encoding.UTF8, "application/x-www-form-urlencoded");
var response = http.PostAsync(url2, content).Result;
//获取Http的状态值
//Console.WriteLine(response.StatusCode);
string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
Console.ReadKey();
}
3. Post的JSON提交格式(url2和url3都能正常访问)
var user = new
{
userName = "admin",
pwd = ""
};
var handler = new HttpClientHandler();
using (var http = new HttpClient(handler))
{
var content = new StringContent(jss.Serialize(user), Encoding.UTF8, "application/json");
var response = http.PostAsync(url3, content).Result;
//获取Http的状态值
//Console.WriteLine(response.StatusCode);
string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
Console.ReadKey();
}
注:以上代码均是官方的给出的标准写法,但存在很严重的问题,当请求量大的时候,会存在不能释放的问题,解决方案见下面。
解决方案:
将HttpClient做成单例的,不用Using,全局只有一个,来解决tcp连接不能释放的问题。
/// <summary>
/// 将HttpClient做成单例的,不用Using,全局只有一个
/// 来解决tcp连接不能释放的问题
/// </summary>
public class HttpClientFactory
{
private static HttpClient _httpClient = null; /// <summary>
/// 静态的构造函数:只能有一个,且是无参数的
/// 由CLR保证,只有在程序第一次使用该类之前被调用,而且只能调用一次
/// 说明: keep-alive关键字可以理解为一个长链接,超时时间也可以在上面进行设置,例如10秒的超时时间,当然并发量太大,这个10秒应该会抛弃很多请求
/// 发送请求的代码没有了using,即这个httpclient不会被手动dispose,而是由系统控制它,当然你的程序重启时,这也就被回收了。
/// </summary>
static HttpClientFactory()
{
_httpClient = new HttpClient(new HttpClientHandler());
_httpClient.Timeout = new TimeSpan(, , );
_httpClient.DefaultRequestHeaders.Connection.Add("keep-alive");
} /// <summary>
/// 对外开放接口
/// </summary>
/// <returns></returns>
public static HttpClient GetHttpClient()
{
return _httpClient;
}
}
优化后调用进行Get和Post请求,代码如下
var http = HttpClientFactory.GetHttpClient();
//1.Get请求
var response1 = http.GetAsync(url1).Result;
var result1 = response1.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Get请求的返回值为:{result1}");
//2. Post请求【application/x-www-form-urlencoded】
var content2 = new StringContent("userName=admin&pwd=123456", Encoding.UTF8, "application/x-www-form-urlencoded");
var response2 = http.PostAsync(url2, content2).Result;
string result2 = response2.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Post请求【application/x-www-form-urlencoded】的返回值为:{result2}");
//3. Post请求 【application/json】
var user = new
{
userName = "admin",
pwd = ""
};
var content3 = new StringContent(jss.Serialize(user), Encoding.UTF8, "application/json");
var response3 = http.PostAsync(url3, content3).Result;
string result3 = response3.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Post请求【application/json】的返回值为:{result3}");
Console.ReadKey();
五. 扩展
将WebClient、WebRequest、HttpClient三个类进行封装,其中HttpClient做成单例的,便于我们调用。
/// <summary>
/// 总结各种请求方式
/// </summary>
public static class RequestWaysHelp
{
#region 01-WebClient的Get请求
/// <summary>
/// WebClient的Get请求
/// </summary>
/// <param name="url">请求地址,含拼接数据,请求格式为:"http://XXXX?userName=admin&pwd=123456";</param>
/// <returns></returns>
public static string WcGet(string url)
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
return wc.DownloadString(url);
}
#endregion #region 02-WebClient的Post请求
/// <summary>
/// WebClient的Post请求
/// 表单提交模式[application/x-www-form-urlencoded]
/// </summary>
/// <param name="url">请求地址,单纯的地址,没有数据拼接</param>
/// <param name="data">请求数据,格式为:"userName=admin&pwd=123456"</param>
/// <returns></returns>
public static string WcPost1(string url, string data)
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
//也可以向表头中添加一些其他东西
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
return wc.UploadString(url, data);
}
#endregion #region 03-WebClient的Post请求
/// <summary>
/// WebClient的Post请求
/// Json提交模式[application/json]
/// </summary>
/// <param name="url">请求地址,单纯的地址,没有数据拼接</param>
/// <param name="data">请求数据,格式为(Json)对象、或者类对象 eg: new {id="1"}</param>
/// <returns></returns>
public static string WcPost2(string url, object data)
{
JavaScriptSerializer jss = new JavaScriptSerializer(); WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
//也可以向表头中添加一些其他东西
wc.Headers.Add("Content-Type", "application/json");
return wc.UploadString(url, jss.Serialize(data));
}
#endregion #region 04-HttpWebRequest的Get请求
/// <summary>
/// HttpWebRequest的Get请求
/// </summary>
/// <param name="url">请求地址,含拼接数据,请求格式为:"http://XXXX?userName=admin&pwd=123456";</param>
/// <returns></returns>
public static string HwGet(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = * ;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
return result;
}
#endregion #region 05-HttpWebRequest的Post请求
/// <summary>
/// HttpWebRequest的Post请求
/// 表单提交模式[application/x-www-form-urlencoded]
/// </summary>
/// <param name="url">请求地址,单纯的地址,没有数据拼接</param>
/// <param name="data">请求数据,格式为:"userName=admin&pwd=123456"</param>
/// <returns></returns>
public static string HwPost1(string url, string data)
{
var request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Timeout = * ;//设置30s的超时
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] data2 = Encoding.UTF8.GetBytes(data);
request.ContentLength = data2.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data2, , data2.Length);
postStream.Close();
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
return result;
}
#endregion #region 06-HttpWebRequest的Post请求
/// <summary>
/// HttpWebRequest的Post请求
/// Json提交模式[application/json]
/// </summary>
/// <param name="url">请求地址,单纯的地址,没有数据拼接</param>
/// <param name="data">请求数据,格式为(Json)对象、或者类对象 eg: new {id="1"}</param>
/// <returns></returns>
public static string HwPost2(string url, object data)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var postData = jss.Serialize(data);
var request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Timeout = * ; //设置30s的超时
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
request.ContentType = "application/json";
request.Method = "POST";
byte[] data2 = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data2.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data2, , data2.Length);
postStream.Close();
string result = "";
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
return result;
}
#endregion #region 07-HttpClient的Get请求
/// <summary>
/// HttpClient的Get请求
/// </summary>
///<param name="url">请求地址,含拼接数据,请求格式为:"http://XXXX?userName=admin&pwd=123456";</param>
/// <returns></returns>
public static string HcGet(string url)
{
var http = HttpClientFactory2.GetHttpClient();
var response1 = http.GetAsync(url).Result;
return response1.Content.ReadAsStringAsync().Result;
}
#endregion #region 08-HttpClient的Post请求
/// <summary>
/// HttpClient的Post请求
/// 表单提交模式[application/x-www-form-urlencoded]
/// </summary>
/// <param name="url">请求地址,单纯的地址,没有数据拼接</param>
/// <param name="data">请求数据,格式为:"userName=admin&pwd=123456"</param>
/// <returns></returns>
public static string HcPost1(string url, string data)
{
var http = HttpClientFactory2.GetHttpClient();
var content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = http.PostAsync(url, content).Result;
return response.Content.ReadAsStringAsync().Result;
}
#endregion #region 09-HttpClient的Post请求
/// <summary>
/// HttpClient的Post请求
/// Json提交模式[application/json]
/// </summary>
/// <param name="url">请求地址,单纯的地址,没有数据拼接</param>
/// <param name="data">请求数据,格式为(Json)对象、或者类对象 eg: new {id="1"}</param>
/// <returns></returns>
public static string HcPost2(string url, object data)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var http = HttpClientFactory2.GetHttpClient();
var content = new StringContent(jss.Serialize(data), Encoding.UTF8, "application/json");
var response = http.PostAsync(url, content).Result;
return response.Content.ReadAsStringAsync().Result;
}
#endregion } /// <summary>
/// 将HttpClient做成单例的,不用Using,全局只有一个
/// 来解决tcp连接不能释放的问题
/// </summary>
public class HttpClientFactory2
{
private static HttpClient _httpClient = null; /// <summary>
/// 静态的构造函数:只能有一个,且是无参数的
/// 由CLR保证,只有在程序第一次使用该类之前被调用,而且只能调用一次
/// 说明: keep-alive关键字可以理解为一个长链接,超时时间也可以在上面进行设置,例如10秒的超时时间,当然并发量太大,这个10秒应该会抛弃很多请求
/// 发送请求的代码没有了using,即这个httpclient不会被手动dispose,而是由系统控制它,当然你的程序重启时,这也就被回收了。
/// </summary>
static HttpClientFactory2()
{
_httpClient = new HttpClient(new HttpClientHandler());
_httpClient.Timeout = new TimeSpan(, , );
_httpClient.DefaultRequestHeaders.Connection.Add("keep-alive");
} /// <summary>
/// 对外开放接口
/// </summary>
/// <returns></returns>
public static HttpClient GetHttpClient()
{
return _httpClient;
}
}
RequestWaysHelp
封装后的调用:
var user = new
{
userName = "admin",
pwd = ""
};
// WebClient的相关测试
{
Console.WriteLine("1.WebClient的相关测试");
var result1 = RequestWaysHelp.WcGet(url1);
Console.WriteLine(result1);
var result2 = RequestWaysHelp.WcPost1(url2, "userName=admin&pwd=123456");
Console.WriteLine(result2);
var result3 = RequestWaysHelp.WcPost2(url3, user);
Console.WriteLine(result3);
}
// HttpWebRequest的相关测试
{
Console.WriteLine("2.HttpWebRequest的相关测试");
var result1 = RequestWaysHelp.HwGet(url1);
Console.WriteLine(result1);
var result2 = RequestWaysHelp.HwPost1(url2, "userName=admin&pwd=123456");
Console.WriteLine(result2);
var result3 = RequestWaysHelp.HwPost2(url3, user);
Console.WriteLine(result3);
}
// HttpClient的相关测试
{
Console.WriteLine("3.HttpClient的相关测试");
var result1 = RequestWaysHelp.HcGet(url1);
Console.WriteLine(result1);
var result2 = RequestWaysHelp.HcPost1(url2, "userName=admin&pwd=123456");
Console.WriteLine(result2);
var result3 = RequestWaysHelp.HcPost2(url3, user);
Console.WriteLine(result3);
}
!
- 作 者 : Yaopengfei(姚鹏飞)
- 博客地址 : http://www.cnblogs.com/yaopengfei/
- 声 明1 : 本人才疏学浅,用郭德纲的话说“我是一个小学生”,如有错误,欢迎讨论,请勿谩骂^_^。
- 声 明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)的更多相关文章
- linux下ssh的几种验证方式
ssh的认证方式有很多种,大概可以概括为以下几类: 1.pam认证方式 在配置文件/etc/ssh/sshd_config中对应参数:UsePAM 2.密钥认证方式 配置文件/etc/ssh/sshd ...
- tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- IIS下PHP的三种配置方式比较
在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...
- 【转】tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- Linux 下wdcp支持两种安装方式
wdcp支持两种安装方式1 源码编译 此安装比较麻烦和耗时,一般是20分钟至一个小时不等,具体视机器配置情况而定2 RPM包安装 简单快速,下载快的话,几分钟就可以完成源码安装(ssh登录服务器,执行 ...
- Apache下PHP的几种工作方式
PHP在Apache中一共有三种工作方式:CGI模式.Apache模块DLL.FastCGI模式. 一.CGI模式 PHP 在 Apache 2中的 CGI模式.编辑Apache 配置文件httpd. ...
- linux下线程的两种封装方式
在网络编程的时候往往需要对Linux下原生的pthread库中的函数进行封装,使其使用起来更加方便,封装方法一般有两种:面向对象和基于对象,下面将分别介绍这两种方式,最后统一分析这两种方式的优缺点: ...
- linux下文件共享的几种常用方式
1. python方式,做一个简单的服务器.默认是开启8000端口. > python -m SimpleHTTPServer 执行命令后,在浏览器上输入该机器IP+8000端口即可 2. sc ...
- Linux下SVN的三种备份方式
原文链接:http://blog.csdn.net/windone0109/article/details/4040772 (本文例子基于FreeBSD/Linux实现,windows环境请自己做出相 ...
随机推荐
- Docker之进入容器(三)
1.简介 经过前面两篇博客的扫盲,大家多多少少对docker有了一个基本的了解,也接触了docker的常用命令.在这篇博客中,我将介绍进入docker容器的几种方式. 2.进入docker中的几种方式 ...
- luffy项目后台drf搭建(1)
一 进入虚拟环境 打开crm,输入命令 workon luffy 虚拟环境使用文档 二 安装基本类库 pip install django pip install PymySQL pip instal ...
- 明天研究下jpa直接像django一样生成
https://blog.csdn.net/yztezhl/article/details/79390714 自动生成 教程-- https://blog.csdn.net/mxjesse/artic ...
- springboot mybatis搭建
非常easy直接写,没有搭建成分 1.目录 2. @RestController public class UserController { @RequestMapping("/hello& ...
- Linux:Day13(上) CentOS系统启动流程
CentOS 5和6的启动流程 Linux:kernel+rootfs kernel:进程管理.内存管理.网络管理.驱动程序.文件系统.安全功能 rootfs: glibc 库:函数集合,functi ...
- Jenkins持续集成实践之java项目自动化部署
关于Linux安装Jenkins可以参考我的这篇博文Ubuntu16.04环境安装jenkins 1.安装部署插件 进入插件管理,并搜索该插件Deploy to container Plugin进行安 ...
- C# — 调用dll出现试图加载不正确格式的程序问题
今天在调用百度dll包时,运行项目出现了如下警告: 修改:鼠标右击项目名称----选择属性----生成-----平台目标-----X64(由于我调用的是X64的dll包,所以这里选择X64,网上许多说 ...
- 类别不平衡问题和Softmax回归
目录 类别不平衡(class-imbalance) Softmax回归模型 类别不平衡(class-imbalance) 当不同类别的训练样本数目差别很大,则会对学习过程造成困扰.如有998个反例,但 ...
- spring @CrossOrigin解决跨域问题
阅读目录: 一.跨域(CORS)支持: 二.使用方法: 1.controller配置CORS 2.全局CORS配置 3.XML命名空间 4.How does it work? 5.基于过滤器的CORS ...
- flask token认证
在前后端分离的项目中,我们现在多半会使用token认证机制实现登录权限验证. token通常会给一个过期时间,这样即使token泄露了,危害期也只是在有效时间内,超过这个有效时间,token过期了,就 ...