如何使用 HttpClientFactory 在基址末尾添加字符串值?
How to add string value at the end of Base Address using HttpClientFactory?
我正在使用 HttpClientFactory 发出 http 请求。
这是我的代码:-
namespace handleDeviceOperations
{
class Program
{
string operationID = String.Empty;
static async Task Main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var services = serviceCollection.BuildServiceProvider();
var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();
//Getting operationID from few other lines of code
var httpClientGetOperations = httpClientFactory.CreateClient("getOperations");
var request1 = await httpClientGetOperations.GetAsync("");
var responseMessage1 = await request1.Content.ReadAsStringAsync();
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddHttpClient("getOperations", options =>
{
options.BaseAddress = new Uri("https://myurl.com/events/");
options.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic","Auth_Value");
});
}
}
如您所见,这是我的基址:"https://myurl.com/events/"
现在,就在此处发出请求之前:-
var httpClientGetOperations = httpClientFactory.CreateClient("getOperations");
var request1 = await httpClientGetOperations.GetAsync("");
我想将 operationID 添加到 Base Address 的末尾,这样 Base Address 就变成这样:-
"https://myurl.com/events/45872254"
//The string at the end will differ each time
这是一个非常必要的步骤,因为请求完全依赖于 operationID 参数。
options.BaseAddress = new Uri("https://myurl.com/");
...
var id = 45872254;
var request1 = await httpClientGetOperations.GetAsync($"events/{id}");
我正在使用 HttpClientFactory 发出 http 请求。
这是我的代码:-
namespace handleDeviceOperations
{
class Program
{
string operationID = String.Empty;
static async Task Main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var services = serviceCollection.BuildServiceProvider();
var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();
//Getting operationID from few other lines of code
var httpClientGetOperations = httpClientFactory.CreateClient("getOperations");
var request1 = await httpClientGetOperations.GetAsync("");
var responseMessage1 = await request1.Content.ReadAsStringAsync();
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddHttpClient("getOperations", options =>
{
options.BaseAddress = new Uri("https://myurl.com/events/");
options.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic","Auth_Value");
});
}
}
如您所见,这是我的基址:"https://myurl.com/events/"
现在,就在此处发出请求之前:-
var httpClientGetOperations = httpClientFactory.CreateClient("getOperations");
var request1 = await httpClientGetOperations.GetAsync("");
我想将 operationID 添加到 Base Address 的末尾,这样 Base Address 就变成这样:-
"https://myurl.com/events/45872254"
//The string at the end will differ each time
这是一个非常必要的步骤,因为请求完全依赖于 operationID 参数。
options.BaseAddress = new Uri("https://myurl.com/");
...
var id = 45872254;
var request1 = await httpClientGetOperations.GetAsync($"events/{id}");