C# Http 请求接口 Get / Post

目录

一、概述

二、创建 Web API 

三、HttpRequestHelper

三、测试

结束


一、概述

get 和 post 请求,最早被用来做浏览器与服务器之间交互HTML和表单的通讯协议,后来又被广泛的扩充到接口格式的定义上,到目前为止,get / post 请求依然应用在各大网站中,比如在用户登录时,调用 get / post 请求将用户名、密码传到服务器,由服务器进行判断,是否允许用户登录,再将结果返回给浏览器,这样就实现了登录的功能。在后期的 pc 软件开发中,get / post 请求偶尔也会用到,做为一个程序员,http 协议也是我们必须要学的知识点。

二、创建 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.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;

public class HttpRequestHelper
{
    /// <summary>
    /// Get请求
    /// </summary>
    /// <param name="url">请求url</param>
    /// <returns></returns>
    public static Task<string> Get(string url)
    {
        var task = Task.Run(() =>
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.Timeout = 5000;

            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();
                }
            }
        });
        return task;
    }

    /// <summary>
    /// 指定Post地址使用Get 方式获取全部字符串
    /// </summary>
    /// <param name="url">请求后台地址</param>
    /// <returns></returns>
    public static Task<string> Post(string url)
    {
        var task = Task.Run(() =>
        {
            string result = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.Timeout = 5000;

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        });
        return task;
    }

    /// <summary>
    /// Post请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <returns></returns>
    public static Task<string> Post(string url, object postData)
    {
        var task = Task.Run(() =>
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Timeout = 5000;

            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();
                }
            }
        });
        return task;
    }

    /// <summary>
    /// 指定Post地址使用Get 方式获取全部字符串
    /// </summary>
    /// <param name="url">请求后台地址</param>
    /// <returns></returns>
    public static Task<string> Post(string url, Dictionary<string, string> dic)
    {
        var task = Task.Run(() =>
        {
            string result = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            //req.ContentType = "application/json";
            req.Timeout = 5000;

            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;
        });
        return task;
    }
}

关于 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

三、测试

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 接口

结束

如果这个帖子对你有所帮助,欢迎 关注 + 点赞 + 留言,或者你有疑问的话,也可以私信给我。

end

猜你喜欢

转载自blog.csdn.net/qq_38693757/article/details/131330883