.Net阿里云API网关SDK

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using aliyun_api_gateway_sdk.Constant;
using aliyun_api_gateway_sdk.Util;

namespace APISDK.Util
{
    public class CallService<T>
    {
        private const string Host = "API网关地址";
        private const string AppKey = "AppKey";
        private const string AppSecret = "APP秘钥";
        public static string DoPostForm(string path, T paraModel, bool isTest)
        {
            var headers = new Dictionary<string, string>();
            var querys = new Dictionary<string, string>();
            var bodys = new Dictionary<string, string>();
            var signHeader = new List<string>();

            //设定Content-Type,根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
            //设定Accept,根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON);

            //如果是调用测试环境请设置
            if (isTest)
            {
                headers.Add(SystemHeader.X_CA_STAGE, "TEST");
            }
            //注意:业务header部分,如果没有则无此行(如果有中文,请做Utf8ToIso88591处理)

            //注意:业务query部分,如果没有则无此行;请不要、不要、不要做UrlEncode处理

            //注意:业务body部分,如果没有则无此行;请不要、不要、不要做UrlEncode处理

            var bobyContent = Newtonsoft.Json.JsonConvert.SerializeObject(paraModel);

            //注意:如果有非Form形式数据(body中只有value,没有key);如果body中是key/value形式数据,不要指定此行
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bobyContent)));
            bodys.Add("", bobyContent);
            //指定参与签名的header            
            signHeader.Add(SystemHeader.X_CA_TIMESTAMP);
            using (var response = HttpUtil.HttpPost(Host, path, AppKey, AppSecret, 30000, headers, querys, bodys, signHeader))
            {
                Console.WriteLine(response.StatusCode);
                Console.WriteLine(response.Method);
                Console.WriteLine(response.Headers);
                var st = response.GetResponseStream();
                if (st != null)
                {
                    var reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                    var result = reader.ReadToEnd();
                    Console.WriteLine(result);
                    return result;
                }
                Console.WriteLine(Constants.LF);
                return "";
            }
        }

        public static string AliPost(string path, string bobyContent, bool isTest)
        {
            var headers = new Dictionary<string, string>();
            var querys = new Dictionary<string, string>();
            var bodys = new Dictionary<string, string>();
            var signHeader = new List<string>();

            //设定Content-Type,根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
            //设定Accept,根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON);

            //如果是调用测试环境请设置
            if (isTest)
            {
                headers.Add(SystemHeader.X_CA_STAGE, "TEST");
            }
            //注意:业务header部分,如果没有则无此行(如果有中文,请做Utf8ToIso88591处理)

            //注意:业务query部分,如果没有则无此行;请不要、不要、不要做UrlEncode处理

            //注意:业务body部分,如果没有则无此行;请不要、不要、不要做UrlEncode处理

            //var bobyContent = Newtonsoft.Json.JsonConvert.SerializeObject(paraModel);

            //注意:如果有非Form形式数据(body中只有value,没有key);如果body中是key/value形式数据,不要指定此行
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bobyContent)));
            bodys.Add("", bobyContent);
            //指定参与签名的header            
            signHeader.Add(SystemHeader.X_CA_TIMESTAMP);
            using (var response = HttpUtil.HttpPost(Host, path, AppKey, AppSecret, 30000, headers, querys, bodys, signHeader))
            {
                Console.WriteLine(response.StatusCode);
                Console.WriteLine(response.Method);
                Console.WriteLine(response.Headers);
                var st = response.GetResponseStream();
                if (st != null)
                {
                    var reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                    var result = reader.ReadToEnd();
                    Console.WriteLine(result);
                    return result;
                }
                Console.WriteLine(Constants.LF);
                return "";
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/willianyy/article/details/83175931