public async Task SendValidToken()
        {
            JwtService service = new JwtService(ValidKey
                                                , "Xari");

            JwtPayload InitialPayload;

            InitialPayload = new JwtPayload {
                { "UserOid", "001" },
                { JwtRegisteredClaimNames.Iat, service.DateToNumber(DateTime.Now).ToString() },
                { JwtRegisteredClaimNames.Iss, "Xari" },
            };

            var StringToken = service.JwtPayloadToToken(InitialPayload);

            var Headers = new RequestHeadersCollection();

            Headers.Add("AuthId", "db1");
            Headers.Add("Authorization", StringToken);
            client = new Client(new NewtonsoftSerializationAdapter(), createHttpClient: (name) => _testServerHttpClientFactory.CreateClient());

            Uri resource = new Uri(UriString);
            var result   = await client.GetAsync <string>(resource, Headers);

            Assert.AreEqual("It's working", result.Body);
        }
    public Request(Uri resource,
                   TRequestBody body,
                   IHeadersCollection headers,
                   HttpRequestMethod httpRequestMethod,
                   IClient client,
                   CancellationToken cancellationToken)
    {
        Body              = body;
        Headers           = headers;
        Resource          = resource;
        HttpRequestMethod = httpRequestMethod;
        CancellationToken = cancellationToken;
        if (Headers == null)
        {
            Headers = new RequestHeadersCollection();
        }
        var defaultRequestHeaders = client?.DefaultRequestHeaders;

        if (defaultRequestHeaders == null)
        {
            return;
        }
        foreach (var kvp in defaultRequestHeaders)
        {
            Headers.Add(kvp);
        }
    }
Example #3
0
        public static async Task <isbn_response> GetISBN(string isbn)
        {
            try
            {
                var url    = $"{host}{path}?isbn={isbn}";
                var client = new Client(new NewtonsoftSerializationAdapter(), new Uri(url));
                client.SetJsonContentTypeHeader();
                //client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                var requesthead = new RequestHeadersCollection();
                requesthead.Add("Authorization", "APPCODE " + appcode);
                var result = await client.GetAsync <isbn_response>(new Uri(url), requesthead);

                NLog.LogManager.GetCurrentClassLogger().Info($"调用ISBN接口成功:{isbn}");
                return(result.Body);
            }catch (Exception e)
            {
                NLog.LogManager.GetCurrentClassLogger().Error($"调用ISBN接口异常:{isbn}");
                return(null);
            }
        }
        public void SendInvalidToken()
        {
            JwtService service = new JwtService(InvalidKey
                                                , "Xari");

            JwtPayload InitialPayload;

            InitialPayload = new JwtPayload {
                { "UserOid", "001" },
                { JwtRegisteredClaimNames.Iat, service.DateToNumber(DateTime.Now).ToString() },
                { JwtRegisteredClaimNames.Iss, "Xari" },
            };

            var StringToken = service.JwtPayloadToToken(InitialPayload);

            var Headers = new RequestHeadersCollection();

            Headers.Add("AuthId", "db1");
            Headers.Add("Authorization", StringToken);
            client = new Client(new NewtonsoftSerializationAdapter(), createHttpClient: (name) => _testServerHttpClientFactory.CreateClient());

            Uri resource = new Uri(UriString);


            string result = string.Empty;

            Assert.Throws <HttpStatusException>(
                () =>
            {
                try
                {
                    result = client.GetAsync <string>(resource, Headers).Result;
                }
                catch (Exception ex)
                {
                    throw ex.InnerException;
                }
            });
        }
        /// <summary>
        /// Build the request headers, override this function if you want to manipulate the content of the headers
        /// </summary>
        /// <returns>Defaults headers including the custom headers passed in this class constructor</returns>
        protected virtual RequestHeadersCollection GetHeaders()
        {
            if (RequestHeders != null)
            {
                return(RequestHeders);
            }

            RequestHeders = new RequestHeadersCollection();
            if (HeadersData == null)
            {
                return(RequestHeders);
            }

            foreach (KeyValuePair <string, string> Current in HeadersData)
            {
                if (!RequestHeders.Contains(Current.Key))
                {
                    RequestHeders.Add(Current.Key, Current.Value);
                }
            }
            return(RequestHeders);
        }