目录
一、概述
二、创建 Web API
三、HttpRequestHelper
三、测试
结束
一、概述
get 和 post 请求,最早被用来做浏览器与服务器之间交互HTML和表单的通讯协议,后来又被广泛的扩充到接口格式的定义上,到目前为止,get / post 请求依然应用在各大网站中,比如在用户登录时,调用 get / post 请求将用户名、密码传到服务器,由服务器进行判断,是否允许用户登录,再将结果返回给浏览器,这样就实现了登录的功能。在后期的 pc 软件开发中,get / post 请求偶尔也会用到,做为一个程序员,http 协议也是我们必须要学的知识点。
二、创建 Web API
如果你只是想参考 Get Post 请求方法,请跳过当前章节
其实 Get Post 请求不光是 Java、PHP 等语言的专利,C# 也可以实现,下面用 C# 大致演示下 web api 的开发流程。
创建 一个 web api 项目,用作后面测试用的接口,关于 Web API 怎么创建项目的,可以参考我之前写的教程:
C# ASP.NET Web Core API (.NET 6.0)_熊思宇的博客-CSDN博客
按上面教程,添加一个 ValuesController 接口后,我这里稍做一些更改
默认:
修改后:
将默认添加的天气预报相关的两个类删除
launchSettings.json 配置如下,在 applicationUrl 这里,你的可能和我的不一样,这个是对外访问的链接,有两个接口,一个是 https,一个是 http,端口号不一样,ip 部分是用来限制登录的,如果开发所有人,就用 0.0.0.0 代替
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:10090",
"sslPort": 44385
}
},
"profiles": {
"WebApplication1": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7281;http://localhost:5252",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
将 ValuesController 代码修改如下
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class ValuesController : ControllerBase
{
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
[HttpPost]
public string Post([FromForm] string json)
{
Console.WriteLine(json);
return "hello, good afternoon";
}
}
}
启动项目,接下来用 Postman 进行测试
1. Get 接口
2.Post 接口
返回了对应的结果,就是成功了
三、HttpRequestHelper
新建一个控制台项目,添加一个类 HttpRequestHelper,第一个版本是一些传统的写法,后面我又做了一点优化,优化的版本在本章节的最下面,这两个版本都可以做参考。
HttpRequestHelper 代码
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// http请求
/// </summary>
public class HttpRequestHelper
{
#region Get
/// <summary>
/// Get请求
/// </summary>
/// <param name="url">请求url</param>
/// <returns></returns>
public static string Get(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.Timeout = 3000;
if (req == null || req.GetResponse() == null)
return string.Empty;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp == null)
return string.Empty;
using (Stream stream = resp.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
/// <summary>
/// Get请求
/// </summary>
/// <param name="url"></param>
/// <param name="headerKey"></param>
/// <param name="headerValue"></param>
/// <returns></returns>
public static string Get(string url, string headerKey, string headerValue)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.Timeout = 3000;
req.Headers[headerKey] = headerValue;
if (req == null || req.GetResponse() == null)
return string.Empty;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp == null)
return string.Empty;
using (Stream stream = resp.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
#endregion
#region Post
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">接口url</param>
/// <returns></returns>
public static string Post(string url)
{
string result = string.Empty;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.Timeout = 3000;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">接口url</param>
/// <param name="postData"></param>
/// <returns></returns>
public static string Post(string url, object postData)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json";
req.Timeout = 3000;
if (req == null)
return string.Empty;
byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData));
//注意:无需手动指定长度 (否则可能会报流未处理完就关闭的异常,因为ContentLength时候会比真实post数据长度大)
//req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp == null)
return string.Empty;
using (Stream stream = resp.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">接口url</param>
/// <param name="dic">参数</param>
/// <returns></returns>
public static string Post(string url, Dictionary<string, string> dic)
{
string result = string.Empty;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Timeout = 3000;
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
/// <summary>
/// Post请求(基于raw)
/// </summary>
/// <param name="url">接口url</param>
/// <param name="json">raw的json参数</param>
/// <returns></returns>
public static async Task<string> Post(string url, string json)
{
using (HttpClient client = new HttpClient())
{
using(var content = new StringContent(json, Encoding.UTF8, "application/json"))
{
using (HttpResponseMessage response = await client.PostAsync(url, content))
{
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
return responseContent;
}
else
{
Console.WriteLine("请求失败,状态码:" + response.StatusCode);
return null;
}
}
}
}
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url"></param>
/// <param name="dic"></param>
/// <param name="headerKey"></param>
/// <param name="headerValue"></param>
/// <returns></returns>
public static string Post(string url, Dictionary<string, string> dic, string headerKey, string headerValue)
{
string result = string.Empty;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Timeout = 3000;
req.Headers[headerKey] = headerValue;
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
#endregion
}
关于 req.ContentType 的赋值,常见用法的有下面这些
/// <summary>
/// HTTP 内容类型(Content-Type)
/// </summary>
public class HttpContentType
{
/// <summary>
/// 资源类型:普通文本
/// </summary>
public const string TEXT_PLAIN = "text/plain";
/// <summary>
/// 资源类型:JSON字符串
/// </summary>
public const string APPLICATION_JSON = "application/json";
/// <summary>
/// 资源类型:未知类型(数据流)
/// </summary>
public const string APPLICATION_OCTET_STREAM = "application/octet-stream";
/// <summary>
/// 资源类型:表单数据(键值对)
/// </summary>
public const string WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
/// <summary>
/// 资源类型:表单数据(键值对)。编码方式为 gb2312
/// </summary>
public const string WWW_FORM_URLENCODED_GB2312 = "application/x-www-form-urlencoded;charset=gb2312";
/// <summary>
/// 资源类型:表单数据(键值对)。编码方式为 utf-8
/// </summary>
public const string WWW_FORM_URLENCODED_UTF8 = "application/x-www-form-urlencoded;charset=utf-8";
/// <summary>
/// 资源类型:多分部数据
/// </summary>
public const string MULTIPART_FORM_DATA = "multipart/form-data";
}
关于 HttpWebRequest,可以参考微软官方文档
HttpWebRequest 类 (System.Net) | Microsoft Learn
2023.10.17 对上面的版本我又进行了优化,有不足的地方欢迎指出
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
/// <summary>
/// http请求
/// </summary>
public class HttpRequestHelper
{
#region Get
/// <summary>
/// Get请求
/// </summary>
/// <param name="url">请求url</param>
/// <returns></returns>
public static string Get(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.Timeout = 3000;
if (req == null) return string.Empty;
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
return reader.ReadToEnd();
}
/// <summary>
/// Get请求
/// </summary>
/// <param name="url"></param>
/// <param name="headerKey"></param>
/// <param name="headerValue"></param>
/// <returns></returns>
public static string Get(string url, string headerKey, string headerValue)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.Timeout = 3000;
req.Headers[headerKey] = headerValue;
if (req == null) return string.Empty;
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
return reader.ReadToEnd();
}
#endregion
#region Post
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">接口url</param>
/// <returns></returns>
public static string Post(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.Timeout = 3000;
if (req == null) return string.Empty;
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
return reader.ReadToEnd();
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">接口url</param>
/// <param name="postData"></param>
/// <returns></returns>
public static string Post(string url, object postData)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json";
req.Timeout = 3000;
if (req == null) return string.Empty;
byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData));
using (Stream reqStream = req.GetRequestStream())
reqStream.Write(data, 0, data.Length);
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
return reader.ReadToEnd();
}
/// <summary>
/// Post请求(基于raw)
/// </summary>
/// <param name="url"></param>
/// <param name="raw"></param>
/// <returns></returns>
public static string Post(string url, string raw)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json"; // 设置合适的 Content-Type
request.Timeout = 3000;
if (request == null) return string.Empty;
byte[] requestBodyBytes = Encoding.UTF8.GetBytes(raw);
request.ContentLength = requestBodyBytes.Length;
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
return reader.ReadToEnd();
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">接口url</param>
/// <param name="dic">参数</param>
/// <returns></returns>
public static string Post(string url, Dictionary<string, string> dic)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Timeout = 3000;
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
reqStream.Write(data, 0, data.Length);
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
return reader.ReadToEnd();
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url"></param>
/// <param name="dic"></param>
/// <param name="headerKey"></param>
/// <param name="headerValue"></param>
/// <returns></returns>
public static string Post(string url, Dictionary<string, string> dic, string headerKey, string headerValue)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Timeout = 3000;
req.Headers[headerKey] = headerValue;
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
reqStream.Write(data, 0, data.Length);
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
return reader.ReadToEnd();
}
#endregion
}
下面是异步版的,推荐使用这个版本
类名:HttpClientHelper
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
public class HttpClientHelper
{
#region 字段
private static HttpClient HttpClientWrapper = new HttpClient();
private static int TimeOut = 3;
#endregion
#region Get
/// <summary>
/// Get 请求
/// </summary>
/// <param name="url">链接</param>
/// <returns></returns>
/// <exception cref="TimeoutException"></exception>
/// <exception cref="Exception"></exception>
public static async Task<string> Get(string url)
{
var response = await HttpClientWrapper.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Get 请求(注意 headerValue 只能单行,不能加入换行符等特殊字符)
/// </summary>
/// <param name="url">链接</param>
/// <param name="headerKey">标题key</param>
/// <param name="headerValue">标题value</param>
/// <returns></returns>
/// <exception cref="TimeoutException"></exception>
/// <exception cref="Exception"></exception>
public static async Task<string> Get(string url, string headerKey, string headerValue)
{
HttpClientWrapper.DefaultRequestHeaders.Clear();
HttpClientWrapper.DefaultRequestHeaders.Add(headerKey, headerValue);
var response = await HttpClientWrapper.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
#endregion
#region Post
/// <summary>
/// Post 请求
/// </summary>
/// <param name="url">链接</param>
/// <param name="content">发送内容</param>
/// <returns></returns>
public static async Task<string> Post(string url, string content)
{
var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
var response = await HttpClientWrapper.PostAsync(url, httpContent);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Post 请求(注意 headerValue 只能单行,不能加入换行符等特殊字符)
/// </summary>
/// <param name="url">链接</param>
/// <param name="content">发送内容</param>
/// <param name="headerKey">标题key</param>
/// <param name="headerValue">标题value</param>
/// <returns></returns>
/// <exception cref="TimeoutException"></exception>
/// <exception cref="Exception"></exception>
public static async Task<string> Post(string url, string content, string headerKey, string headerValue)
{
HttpClientWrapper.DefaultRequestHeaders.Clear();
HttpClientWrapper.DefaultRequestHeaders.Add(headerKey, headerValue);
var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
var response = await HttpClientWrapper.PostAsync(url, httpContent);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Post 请求(注意 headers 参数只能单行,不能加入换行符等特殊字符)
/// </summary>
/// <param name="url">链接</param>
/// <param name="headers">标题</param>
/// <param name="content">发送内容</param>
/// <returns></returns>
/// <exception cref="TimeoutException"></exception>
/// <exception cref="Exception"></exception>
public static async Task<string> Post(string url, string content, Dictionary<string, string> headers)
{
HttpClientWrapper.DefaultRequestHeaders.Clear();
foreach (var header in headers)
HttpClientWrapper.DefaultRequestHeaders.Add(header.Key, header.Value);
var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
var response = await HttpClientWrapper.PostAsync(url, httpContent);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Post 请求(注意 headers 参数只能单行,不能加入换行符等特殊字符)
/// </summary>
/// <param name="url"></param>
/// <param name="parameters">参数合集</param>
/// <param name="headers"></param>
/// <returns></returns>
/// <exception cref="TimeoutException"></exception>
/// <exception cref="Exception"></exception>
public static async Task<string> Post(string url, Dictionary<string, string> parameters, Dictionary<string, string> headers)
{
HttpClientWrapper.DefaultRequestHeaders.Clear();
foreach (var header in headers)
HttpClientWrapper.DefaultRequestHeaders.Add(header.Key, header.Value);
var formContent = new FormUrlEncodedContent(parameters);
var response = await HttpClientWrapper.PostAsync(url, formContent);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
#endregion
#region 构造函数
static HttpClientHelper()
{
HttpClientWrapper.Timeout = TimeSpan.FromSeconds(TimeOut);
}
private HttpClientHelper() { }
#endregion
}
三、测试
1.Get 请求
在 Program 类中添加下面代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HttpTest
{
internal class Program
{
static void Main(string[] args)
{
Test();
Console.ReadKey();
}
static async void Test()
{
string url = "http://localhost:5252/Values";
try
{
string result = await HttpRequestHelper.Get(url);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine("错误:\n{0}", ex.Message);
}
}
}
}
运行:
将地址故意写错,再次执行,就能捕捉到 “无法连接到服务器” 的错误
2.Post 请求
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HttpTest
{
internal class Program
{
static void Main(string[] args)
{
Test();
Console.ReadKey();
}
static async void Test()
{
string url = "http://localhost:5252/Values";
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("json", "你好");
try
{
string result = await HttpRequestHelper.Post(url, dic);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine("错误:\n{0}", ex.Message);
}
}
}
}
运行:
使用 Post 接口需要注意的是,参数的名字必须要和 web api 参数的名字一模一样,比如当前 post 接口的参数名字是 “json”
那么在 dic 添加的键值对,key 也必须是 “json”,如果不是的话,会无法访问到 post 接口
结束
如果这个帖子对你有所帮助,欢迎 关注 + 点赞 + 留言,或者你有疑问的话,也可以私信给我。文章来源:https://www.toymoban.com/news/detail-668031.html
end文章来源地址https://www.toymoban.com/news/detail-668031.html
到了这里,关于C# Http 请求接口 Get / Post的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!