引用System.Net.Http.HttpClient库,它是NuGet上的HttpClient包或.NET 4.5库的一部分。
WPF 用户界面在异步 HttpClient 调用期间无响应
在 .NET 6 WPF 应用程序中,启动和取消后台异步任务(通过两个按钮)。此任务使用 HttpClient 从 Web API 下载分页数据。用户界面是一个空白...
如何为我的 HttpClient PostAsync 第二个参数设置 HttpContent?
public static async Task GetData(string url, string data) { UriBuilder fullUri = new UriBuilder(url); 如果(!string.IsNullOrEmpty(数据)) fullUri.Query = 数据; HTTP...
在实际调用外部函数之前,我正在模拟内部函数之一。 我正在模拟一个 httpwrpapper 类方法。 问题是我的模拟方法没有返回响应。 下面是...
我在我的 UWP 应用程序中使用 Refit 客户端和依赖注入的 Polly 策略设置了以下服务集合: var serviceCollection = new ServiceCollection(); 服务集合 .
PostAsJsonAsync 在<Tvalue> 有空字段时不调用控制器
我的 TValue 对象 有外键相关对象,在发布时有空值;我有在存储库中设置 FK 对象的逻辑。我面临的问题是......
C# net 版本 4.8 HttpClient:无法创建 SSL/TLS 安全通道
我想向“https://www.mexc.co/”发送一个简单的请求。 1.net版本4.8 2.本网站使用浏览器轻松打开 3.没有任何ssl验证错误或警告 结果:重新...
由于 HttpWebRequest 已过时,我正在升级到 .NET 6 中的等效项。 我不明白为什么我会收到未经授权的 401。我可以获得不记名令牌
为 HttpRequestMessage 替换 URL 参数中的参数
如何替换 C# HttpRequestMessage URL 中的参数? 我有以下代码, 使用 (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://MyService/Ids/63847/cr...
streamReader.ReadToEndAsync() 改变消息内容
我有一个 .Net Core 3.1 Web API 需要与外部服务交互,该外部服务提供来自 WCF 服务的多部分/相关响应,上下文中的附件如下块。 ...
为什么我尝试从 zip 文件发布内容而不是普通文本文件时得到 404?
我有一个 MVC 跨 Web API 应用程序,它有一个 ApiFileController,标题为: [产生(“应用程序/json”)] [路线(“API/文件”)] 公共类 ApiFileController : ApiBaseController 它...
您可以将 httpclients 添加到服务中而不必指定每个吗
我有一个使用 Microsoft.Extensions.Http 的 .NET 6 应用程序。一个典型的服务由接口和实现定义如下: 公共接口 IAddressService { 任务 我有一个使用 Microsoft.Extensions.Http 的 .NET 6 应用程序。一个典型的服务由一个接口和实现定义如下: public interface IAddressService { Task<List<Address>?> List(); Task<List<Address>?> List(int appid); Task<Address?> Get(int id); Task<HttpResponseMessage?> Update(Address Address); Task<Address?> Add(Address Address); } public class AddressService : IAddressService { private readonly HttpClient _httpClient; public AddressService(HttpClient httpClient) { _httpClient = httpClient; } async Task<List<Address>?> IAddressService.List() { List<Address>? ret; var response = await _httpClient.GetAsync($"api/Addresses"); if (response.IsSuccessStatusCode) { ret = await JsonSerializer.DeserializeAsync<List<Address>> (await response.Content.ReadAsStreamAsync(), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); } else { ret = new List<Address>(); } return ret; } 等等。我对数据库的每个表都有一个。在配置服务中,我像这样连接它们: services.AddTransient<MPSHttpHandler>(); services.AddHttpClient<IAddressService, AddressService>(client => { client.BaseAddress = new Uri(Configuration["Settings:ApiAddress"] ?? ""); }).AddHttpMessageHandler<MPSHttpHandler>(); 我必须指定每个服务(客户端)。我想知道是否有办法做类似的事情: var clients = typeof(Program).Assembly .GetTypes() .Where(t => t.Name.EndsWith("Service")) .ToList(); clients.ForEach(client => { services.AddHttpClient<client.Interface, client.Implementation>(client => { client.BaseAddress = new Uri(Configuration["Settings:ApiAddress"] ?? ""); }).AddHttpMessageHandler<MPSHttpHandler>(); }); 我不知道如何将客户端类型设置为可以在调用 AddHttpClient 时使用它们的表单。有没有办法做到这一点? 谢谢, 吉姆 **** 更新 **** 我按照@Guru Stron 的示例实现了该方法。我更改了类型以匹配 AddHttpClient 的签名。我创建了静态类如下: public static class MyHttpClientRegExts { public static IServiceCollection AddCustomHttpClient<TClient, TImplementation>(this IServiceCollection services, IConfiguration Configuration) { services.AddHttpClient<TClient, TImplementation>(client => { client.BaseAddress = new Uri(Configuration["Settings:ApiAddress"] ?? ""); }).AddHttpMessageHandler<MPSHttpHandler>(); return services; } } 我在客户端 => 的 lambda 操作中收到以下错误 CS1643: Not all code paths return a value in method of type 'func<HttpClient, TImplementaion>' 这告诉我它正在使用过载 AddHttpClient<TClient,TImplementation>(IServiceCollection, Func<HttpClient,TImplementation>) 而不是过载: AddHttpClient<TClient,TImplementation>(IServiceCollection, Action<HttpClient>) 有办法解决这个问题吗? 谢谢, 吉姆 你可以通过构造和调用泛型方法来使用更多的反射(AddHttpClient 实现做了一些内部的东西所以重用然后尝试重复会更好)。最简单的方法是将设置移动到单独的静态类中: public static class MyHttpClientRegExts { public static IServiceCollection AddCustomHttpClient<TService, TImpl>(this IServiceCollection services, IConfiguration Configuration) { services.AddHttpClient<IAddressService, AddressService>(client => { client.BaseAddress = new Uri(Configuration["..."] ?? ...); }) .AddHttpMessageHandler<MPSHttpHandler>(); return services; } } 然后用它来设置每项服务。让你开始的东西: var types = typeof(Program).Assembly .GetTypes() .Where(t => t is { IsAbstract: false, IsClass: true } && t.Name.EndsWith("Service")) // find implementation types .ToList(); // find the setup method var method = typeof(MyHttpClientRegExts).GetMethod(nameof(MyHttpClientRegExts.AddCustomHttpClient)); foreach (var implType in types) { // find interface type to register, maybe will need some refinement var interfaceType = implType.GetInterfaces().Single(i => i.Name.EndsWith("Service")); // create closed generic method and invoke to register the type pair method.MakeGenericMethod(interfaceType, implType) .Invoke(null, new object?[] { services, Configuration }); }
Parallel.ForEach 循环 ThreadAbortException 已超过最大等待/睡眠/加入时间
我在 .NET 4.8 应用程序中使用 Parallel.ForEach 循环从 Web API 并行下载多个文件。 问题是有时我会得到一个 ThreadAbortException / The Maximum Wait/Slee ...
是否可以将具有 HttpClientFactory 的 http 客户端实例化为 gRPC 客户端?
为每次调用服务器创建一个 HttpClient 似乎不是一个好习惯。这是在这里评论:https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ 阅读文档...
ASP.NET Core 5 Web API:第一次 REST 调用响应缓慢
我的 ASP.NET Core 5.0 Web API 在 Windows 7 SP1 上运行。 第一次 HttpClient REST 调用(来自 .NET 5.0 客户端)响应非常慢,下一次调用非常快,过了一会儿(大约 2 分钟......
ASP.NET Core 5 Web API:第一次 REST 调用响应缓慢
我的 ASP.NET Core 5.0 Web API 在 Windows 7 SP1 上运行。 第一次 HttpClient REST 调用(来自 .NET 5.0 客户端)响应非常慢,下一次调用非常快,过了一会儿(大约 2 分钟......
httpClient PostAsJsonAsync 在 Framework 4.6 中不工作
我不知道它是什么,但是当我在调试模式下运行我的项目并点击这一行时: var response = await httpClient.PostAsJsonAsync(url, payload); 它挂了一秒钟,然后就消失了
很难将成功的 Postman 请求转换为 C# 中的成功请求。使用 HttpClient 显示我的代码,但也尝试使用 PostSharp 和 HttpRequest。我正在使用本地...
如何使用 System.Net.Http.HttpClient 的基本身份验证?
我正在尝试在 c# .net 核心中实现一个休息客户端,它需要首先进行基本身份验证,然后在后续请求中利用 Bearer 令牌。 当我尝试在
如何在 .NET 中使用 OpenSSL 密钥和 CSR 文件调用 SOAP API?
我正在尝试调用我们供应商之一的 SOAP API。我们被要求使用 OpenSSL 生成密钥和 CSR 文件,并将生成的 CSR 文件提供给供应商。然后供应商将生成一个证书...
我有像这样定义的与 HttpClient 相关的 Polly 设置 公共静态 IHttpClientBuilder WithResiliencePolicies( 这个 IHttpClientBuilder clientBuilder, 弹性策略设置?设置...