Restful API Postman工具测试和客户端调用测试(二)

1.Postman工具测试

Postman下载地址:https://www.postman.com/downloads/

第一步,创建接口请求

请求方式为POST

 首先在headers中,设置Content-Type为applicationon/json

                            KEY:Content-Type  VALUE:application/json

 

在body中,raw中输入JSON数据结构,以及勾选JSON(application/json)

有数据证明接口是OK的。

参考文档:https://www.cnblogs.com/yangtianruo/p/5667146.html

那下一步进行客户端调用和反馈结果进行解析:

客户端调用:

我们定义一个Restful 客户端的帮助类RestClient,用于和Restful 服务端交互,如图所示:

帮助类代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace RestFulClient
{
    /// <summary>
    /// 请求类型
    /// </summary>
    public enum EnumHttpVerb
    {
        GET,
        POST,
        PUT,
        DELETE
    }

    public class RestClient
    {
        #region 属性
        /// <summary>
        /// 端点路径
        /// </summary>
        public string EndPoint { get; set; }

        /// <summary>
        /// 请求方式
        /// </summary>
        public EnumHttpVerb Method { get; set; }

        /// <summary>
        /// 文本类型(1、application/json 2、txt/html)
        /// </summary>
        public string ContentType { get; set; }

        /// <summary>
        /// 请求的数据(一般为JSon格式)
        /// </summary>
        public string PostData { get; set; }
        #endregion

        #region 初始化
        public RestClient()
        {
            EndPoint = "";
            Method = EnumHttpVerb.GET;
            ContentType = "application/json";
            PostData = "";
        }

        public RestClient(string endpoint)
        {
            EndPoint = endpoint;
            Method = EnumHttpVerb.GET;
            ContentType = "application/json";
            PostData = "";
        }

        public RestClient(string endpoint, EnumHttpVerb method)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "application/json";
            PostData = "";
        }

        public RestClient(string endpoint, EnumHttpVerb method, string postData)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "application/json";
            PostData = postData;
        }
        #endregion

        #region 方法
        /// <summary>
        /// http请求(不带参数请求)
        /// </summary>
        /// <returns></returns>
        public string HttpRequest()
        {
            return HttpRequest("");
        }

        /// <summary>
        /// http请求(带参数)
        /// </summary>
        /// <param name="parameters">parameters例如:?name=LiLei</param>
        /// <returns></returns>
        public string HttpRequest(string parameters)
        {
            var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);

            request.Method = Method.ToString();
            request.ContentLength = 0;
            request.ContentType = ContentType;

            if (!string.IsNullOrEmpty(PostData) && Method == EnumHttpVerb.POST)
            {
                var bytes = Encoding.UTF8.GetBytes(PostData);
                request.ContentLength = bytes.Length;

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = string.Format("请求数据失败. 返回的 HTTP 状态码:{0}", response.StatusCode);
                    throw new ApplicationException(message);
                }

                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }
                return responseValue;
            }
        }
        #endregion
    }
}

接下来我们验证resful 服务端当中实例

 class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Restful客户端Demo测试";

            RestClient client = new RestClient();
            client.EndPoint = @"http://127.0.0.1:7788/";

            client.Method = EnumHttpVerb.POST;
            Task task = new Task();
            task.Id = 123456789123456789;
            task.TaskType = 1;
            client.PostData = JsonConvert.SerializeObject(task);
            var updatePostJson = client.HttpRequest("TaskInfoInsert/Task");
            Console.WriteLine("POST返回结果:" + updatePostJson);
        }
    }

说明:Task类就是数据库字段一样的实体类。因为这里是客户端,重新定义接收或者传参的实体类(字段必须跟数据库一至)

 [Serializable]
    public class Task
    {
        public long? Id { get; set; }

        public int TaskType { get; set; }
    }

参考文档:http://www.cnblogs.com/xuliangxing/p/8735552.html 

返回的Json字符串:

{ "Id": 789456139, "Message": "存在重复ID","Status": 0}

如何进行解析返回的Json字符串,其实很简单

不管是序列化还是反序列化,都会用到 Newtonsoft.Json这个DLL

那么直接传入实体类还有需要解析的JSON字符串这样就OK了

 Task tk = JsonConvert.DeserializeObject<Task>(updatePostJson);

最后通过 实体.属性取出具体值

Console.WriteLine("解析json:ID:" + tk.Id + "任务类型:" + tk.TaskType);

参考文档:https://www.cnblogs.com/zoujinhua/p/10330066.html

 

上面的实现结合网上例子和个人总结,如有侵权。删除!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小丫头呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值