更新时间:2024-02-18 GMT+08:00

C#

本节以Visual Studio工具为例,介绍如何在C#环境中集成API请求签名的SDK。您可以直接导入示例工程体验,然后参考调用说明部分将签名SDK集成到您的应用中。

准备环境

获取并安装Visual Studio,可至Visual Studio官方网站下载。

获取SDK

点此下载SDK与Demo

解压后目录结构如下:

名称

说明

apigateway-signature\Signer.cs

SDK代码

apigateway-signature\HttpEncoder.cs

sdk-request\Program.cs

签名请求示例代码

csharp.sln

工程文件

licenses\license-referencesource

第三方库license文件

打开工程

双击SDK包中的“csharp.sln”文件,打开工程。其中,apigateway-signature项目为实现签名算法的共享库,可用于.Net Framework与.Net Core项目。“sdk-request”项目为调用示例。

请求签名与API调用

  1. 在工程中引入sdk。

    1
    2
    3
    4
    5
    6
    using System;
    using System.Net;
    using System.IO;
    using System.Net.Http;
    using System.Threading;
    using APIGATEWAY_SDK;
    

  2. 生成一个新的Signer, 填入AK和SK。

    1
    2
    3
    4
    5
    Signer signer = new Signer();
    // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables.
    // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
    signer.Key = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK");
    signer.Secret = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK");
    

  3. 生成一个新的Request,指定域名、方法名、请求uri和body。

    1
    2
    3
    4
    //The following example shows how to set the request URL and parameters to query a VPC list.
    HttpRequest r = new HttpRequest("GET", new Uri("https://{service}.region.example.com/v1/77b6a44cba5**********9a8ff44fd/vpcs?limit=1"));
    //Add a body if you have specified the PUT or POST method. Special characters, such as the double quotation mark ("), contained in the body must be escaped.
    r.body = "";
    

  4. 添加需要签名的其他头域,或者其他用途的头域,如多项目场景中添加X-Project-Id,或者全局服务场景中添加X-Domain-Id。

    1
    2
    //Add header parameters, for example, X-Domain-Id for invoking a global service and X-Project-Id for invoking a project-level service.
    r.headers.Add("X-Project-Id", "xxx");
    

  5. 进行签名,执行此函数会生成一个新的HttpWebRequest,并在请求参数中添加用于签名的X-Sdk-Date头和Authorization头。

    如果您使用HTTP Client,可以从请求中获取头部信息使用。关于头部信息,请参考AK/SK签名认证算法详解
    1
    HttpWebRequest req = signer.Sign(r);
    

  6. 访问API,查看访问结果。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    try
    {
        var writer = new StreamWriter(req.GetRequestStream());
        writer.Write(r.body);
        writer.Flush();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        var reader = new StreamReader(resp.GetResponseStream());
        Console.WriteLine(reader.ReadToEnd());
    }
    catch (WebException e)
    {
        HttpWebResponse resp = (HttpWebResponse)e.Response;
        if (resp != null)
        {
            Console.WriteLine((int)resp.StatusCode + " " + resp.StatusDescription);
            var reader = new StreamReader(resp.GetResponseStream());
            Console.WriteLine(reader.ReadToEnd());
        }
        else
        {
            Console.WriteLine(e.Message);
        }
    }
    Console.WriteLine("----------------");