所以我有一个脚本,它用WebRequest类发送请求。我想将它更改为使用HttpClient类。
建议有一个单例HttpClient类,因为多次调用它可能耗尽套接字池。但是,以HttpClient作为单例将意味着我无法每次更改请求的头信息。每次我都想改变标题。
如何实现具有每次更改请求头的能力的单例HttpClient类?
发布于 2022-10-12 13:22:04
例如,职位请求:
using HttpRequestMessage msg = new HttpRequestMessage();
msg.RequestUri = new Uri("...");
msg.Method = HttpMethod.Post;
msg.Headers.Add("x-my-custom-header", 123);
msg.Content = new StringContent("some string content");
await httpClient.SendAsync(msg, cancellationToken);https://stackoverflow.com/questions/74042272
复制相似问题