示例#1
0
        public static async Task <HttpResponseMessage> SendAsync(this HttpClient client, object url, object method, object headers = null, object content = null, CancellationToken cancellationToken = default)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (url is null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            if (!(url is Uri) && !(url is string))
            {
                throw new ArgumentOutOfRangeException(nameof(url), $"For '{url}', only the following types are supported: Uri, string");
            }
            if (method is null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (!(method is HttpMethod) && !(method is string))
            {
                throw new ArgumentOutOfRangeException(nameof(method), $"For '{method}', only the following types are supported: HttpMethod, string");
            }

            using var request = new HttpRequestMessage(method is string methodString ? new HttpMethod(methodString) : (HttpMethod)method, url is string urlString ? new Uri(urlString) : (Uri)url);
            if (!(content is null) && !ContentConverters.TryConvert(request, content))
            {
                throw new ArgumentOutOfRangeException(nameof(content), $"For '{content}', only the following types are supported: {ContentConverters.SupportedTypesString}");
            }
            if (!(headers is null) && !HeaderConverters.TryConvert(request, headers))
            {
                throw new ArgumentOutOfRangeException(nameof(headers), $"For '{headers}', only the following types are supported: {HeaderConverters.SupportedTypesString}");
            }
            return(await client.SendAsync(request, cancellationToken));
        }
示例#2
0
        private static void AddResource(TmodFile modFile, string relPath, string filePath)
        {
            using (var src = File.OpenRead(filePath))
                using (var dst = new MemoryStream()) {
                    if (!ContentConverters.Convert(ref relPath, src, dst))
                    {
                        src.CopyTo(dst);
                    }

                    modFile.AddFile(relPath, dst.ToArray());
                }
        }
示例#3
0
        private void AddResource(BuildingMod mod, string resource)
        {
            var relPath = resource.Substring(mod.path.Length + 1);

            using (var src = File.OpenRead(resource))
                using (var dst = new MemoryStream()) {
                    if (!ContentConverters.Convert(ref relPath, src, dst))
                    {
                        src.CopyTo(dst);
                    }

                    mod.modFile.AddFile(relPath, dst.ToArray());
                }
        }
        public static async Task <HttpResponseMessage> SendAsync(this HttpClient client, object url, object method, object headers = null, object content = null, CancellationToken cancellationToken = default)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (url is null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            if (!(url is Uri) && !(url is string))
            {
                throw new ArgumentOutOfRangeException(nameof(url), $"For '{url}', only the following types are supported: Uri, string");
            }
            if (method is null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (!(method is HttpMethod) && !(method is string))
            {
                throw new ArgumentOutOfRangeException(nameof(method), $"For '{method}', only the following types are supported: HttpMethod, string");
            }

            using var request = new HttpRequestMessage(method is string methodString ? new HttpMethod(methodString) : (HttpMethod)method, url is string urlString ? new Uri(urlString) : (Uri)url);
            if (!(content is null) && !ContentConverters.TryConvert(request, content))
            {
                throw new ArgumentOutOfRangeException(nameof(content), $"For '{content}', only the following types are supported: {ContentConverters.SupportedTypesString}");
            }
            if (!(headers is null) && !HeaderConverters.TryConvert(request, headers))
            {
                throw new ArgumentOutOfRangeException(nameof(headers), $"For '{headers}', only the following types are supported: {HeaderConverters.SupportedTypesString}");
            }
#if DEBUG
            if (!(headers is null) && HttpMessageInvoker_Handler_FieldInfo.GetValue(client) is HttpClientHandler handler && handler.UseCookies)
            {
                using var r = new HttpRequestMessage { Content = new StringContent(string.Empty) };
                HeaderConverters.TryConvert(r, headers);
                if (r.Headers.Contains("Cookie"))
                {
                    System.Diagnostics.Debug.Assert(false, "Don't set cookies by headers if HttpClientHandler.UseCookies is true.");
                }
            }
#endif
            return(await client.SendAsync(request, cancellationToken));
        }