一. 前言
前端调用有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 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。