zoukankan      html  css  js  c++  java
  • 简单编写的接口请求通用类

    朋友对接酷家乐接口,帮忙简单写的一个测试的请求接口通用类。

    代码如下:

            //时间戳
            public static long timeStamp = 0L;
            //账号信息
            public const string appKey = "appKey";
            public const string Appuid = "Appuid ";
            public const string appSecret = "appSecret ";
            public static string sign = string.Empty;
    
            //post通用方法
            public static string Post(string url, string requestBody)
            {
                try
                {
                    if (string.IsNullOrEmpty(url))
                    {
                        return "地址异常,请检查!";
                    }
                    timeStamp = GetTimeStamp();
                    //md5加密
                    string sign = (appSecret + appKey + timeStamp).MDString();
                    StringBuilder apiBuilder = new StringBuilder();
                    apiBuilder.Append(url)
                        // 鉴权的参数作为URL Query Param
                        .Append("?appkey=").Append(appKey)
                        .Append("&timestamp=").Append(timeStamp)
                        .Append("&sign=").Append(sign);
                    var client = new RestClient(apiBuilder.ToString());
                    client.Timeout = -1;
                    var request = new RestRequest(Method.POST);
                    request.AddHeader("Content-Type", "application/json");
                    request.AddParameter("application/json", requestBody, ParameterType.RequestBody);
                    IRestResponse response = client.Execute(request);
                    return response.Content;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }           
            }
    
            //get通用方法
            public static string Get(string url, string requestBody)
            {
                try
                {
                    if (string.IsNullOrEmpty(url))
                    {
                        return "接口地址不能为空!";
                    }
                    timeStamp = GetTimeStamp();
                    //md5加密
                    string sign = (appSecret + appKey + timeStamp).MDString();
                    StringBuilder apiBuilder = new StringBuilder();
                    apiBuilder.Append(url)
                        // 鉴权的参数作为URL Query Param
                        .Append("?appkey=").Append(appKey)
                        .Append("&timestamp=").Append(timeStamp)
                        .Append("&sign=").Append(sign);
                    var client = new RestClient(apiBuilder.ToString());
                    client.Timeout = -1;
                    var request = new RestRequest(Method.GET);
                    request.AddHeader("Content-Type", "application/json");
                    request.AddParameter("application/json", requestBody, ParameterType.RequestBody);
                    IRestResponse response = client.Execute(request);
                    return response.Content;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
                
            }
    
            //获取时间戳
            public static long GetTimeStamp()
            {
                TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                return Convert.ToInt64(ts.TotalMilliseconds);
            }
        }
    

      需要引用两个类库:

           1、RestSharp

           2、Masuit.Tools.Core

           账号和密码都需要企业注册,仅供参考使用。

  • 相关阅读:
    http服务详解(1)——一次完整的http服务请求处理过程
    Mysql数据库的二进制安装和基础入门操作
    DNS—正、反向解析;委派;主从;子域;转发;智能dns等的实现
    CA认证和颁发吊销证书
    Maven构建Web项目
    MySQL,查看连接数和状态等
    Maven编译出现“[ERROR] java.lang.OutOfMemoryError: Java heap space”
    Map集合
    List集合
    Myecplise反编译工具安装
  • 原文地址:https://www.cnblogs.com/sailing92/p/14131973.html
Copyright © 2011-2022 走看看