C#语言SDK发送长内容时,报错“Invalid URI: The Uri string is too long”

问题场景

C#语言SDK发送长内容时,报错“Invalid URI: The Uri string is too long”。

解决方案

登录阿里云openAPI生成调试代码。

开启“Common模式生成示例“开关,并填写参数。

image

获取到代码后,需要将URL传参改为Body传参。

请参考截图,两处重点改动:

image

修改后代码:

// This file is auto-generated, don't edit it. Thanks.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Tea;
using Tea.Utils;


namespace AlibabaCloud.SDK.Sample
{
    public class Sample
    {

        /**
         * 使用AK&SK初始化账号Client
         * @return Client
         * @throws Exception
         */
        public static AlibabaCloud.OpenApiClient.Client CreateClient()
        {
            // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
            // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378671.html。
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = "xxxxxx",
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = "xxxxxx",
            };
            // Endpoint 请参考 https://api.aliyun.com/product/Dm
            config.Endpoint = "dm.aliyuncs.com";
            return new AlibabaCloud.OpenApiClient.Client(config);
        }

        /**
         * API 相关
         * @param path params
         * @return OpenApi.Params
         */
        public static AlibabaCloud.OpenApiClient.Models.Params CreateApiInfo()
        {
            AlibabaCloud.OpenApiClient.Models.Params params_ = new AlibabaCloud.OpenApiClient.Models.Params
            {
                // 接口名称
                Action = "SingleSendMail",
                // 接口版本
                Version = "2015-11-23",
                // 接口协议
                Protocol = "HTTPS",
                // 接口 HTTP 方法
                Method = "POST",
                AuthType = "AK",
                Style = "RPC",
                // 接口 PATH
                Pathname = "/",
                // 接口请求体内容格式
                ReqBodyType = "formdata",
                // 接口响应体内容格式
                BodyType = "json",
            };
            return params_;
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.OpenApiClient.Client client = CreateClient();
            AlibabaCloud.OpenApiClient.Models.Params params_ = CreateApiInfo();
            // query params
            Dictionary<string, object> queries = new Dictionary<string, object>() { };
            queries["AccountName"] = "xxxxxx";
            queries["AddressType"] = 1;
            queries["ReplyToAddress"] = false;
            queries["ToAddress"] = "xxxxxx";
            queries["Subject"] = "test2";
            queries["HtmlBody"] = "test长内容";
            // runtime options
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            AlibabaCloud.OpenApiClient.Models.OpenApiRequest request = new AlibabaCloud.OpenApiClient.Models.OpenApiRequest
            {
                Body = queries,
            };
            // 复制代码运行请自行打印 API 的返回值
            // 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
            client.CallApi(params_, request, runtime);
        }


    }
}