private string GetMetadataToken()

in aliyun-net-credentials/Provider/EcsRamRoleCredentialProvider.cs [267:307]


        private string GetMetadataToken(IConnClient client)
        {
            try
            {
                HttpRequest httpRequest = new HttpRequest("http://" + MetadataServiceHost + UrlInMetadataToken);
                httpRequest.Method = MethodType.PUT;
                httpRequest.ConnectTimeout = this.connectTimeout;
                httpRequest.ReadTimeout = this.readTimeout;
                httpRequest.Headers.Add("X-aliyun-ecs-metadata-token-ttl-seconds", metadataTokenDuration.ToString());

                HttpResponse httpResponse;
                try
                {
                    httpResponse = client.DoAction(httpRequest);
                }
                catch (Exception ex)
                {
                    throw new CredentialException("Failed to connect ECS Metadata Service: " + ex.GetType() + ": " +
                                                  ex.Message);
                }

                if (httpResponse != null && httpResponse.Status != 200)
                {
                    throw new CredentialException(EcsMetadataFetchErrorMsg + " HttpCode=" + httpResponse.Status +
                                                  ", ResponseMessage=" + httpResponse.GetHttpContentString());
                }

                return httpResponse.GetHttpContentString();
            }
            catch (Exception ex)
            {
                if (this.disableIMDSv1)
                {
                    throw new CredentialException(
                        "Failed to get token from ECS Metadata Service, and fallback to IMDS v1 is disabled via the disableIMDSv1 configuration is turned on. Original error: " +
                        ex.Message);
                }

                return null;
            }
        }