HttpClient我想通过我自己的 C# API向外部 API 发送请求。我正在使用 dot net 5 和基本身份验证。这是我的代码:
var client = new HttpClient
{
BaseAddress = new Uri(baseUrl)
};
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Put, "apiUrl");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var param = JsonConvert.SerializeObject(new
{
param1="",
param2=""
});
requestMessage.Content = new StringContent(param, Encoding.UTF8, "application/json");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{user}:{pass}")));
HttpResponseMessage response = await client.SendAsync(requestMessage);
Run Code Online (Sandbox Code Playgroud)
通常,我像这样发送http请求。但现在我遇到了一些问题。
从我的请求标头中删除行授权标头后HttpResponseMessage response = await client.SendAsync(requestMessage);,我收到 UnAuthorized http 错误。
我知道,当发生重定向时,出于安全原因,授权标头被删除。我也不确定外部 API 中的重定向。
我将HttpClientHandlerwith添加AllowAutoRedirect = false到我的HttpClient
var handler = new HttpClientHandler() …Run Code Online (Sandbox Code Playgroud) c# httpclient unauthorized basic-authentication www-authenticate
我需要用给定的数字生成一个具有 5 个长度的唯一代码。换句话说,我需要将自然数编码为 5 长度的唯一代码
我想给客户固定长度的可记忆代码,并将序列号保留在数据库中,并在需要时进行编码或解码。
给定的数字可以在 1 到 9999999 的范围内。但结果总是必须是 5 个长度。
例如
1 => a56er 或 2 => c7gh4
唯一性很重要,我在谷歌上搜索了很多,但找不到解决方案。