使用 HttpClient 时如何解决 302 错误?

How do I resolve 302 Error when using HttpClient?

我创建了一个自定义 DNN 模块,它使用 HTTPClient 将信息发送到外部 API。我的HttpClient方法如下:

public static async Task<string> CreatePayerResponse()
    {
        var credentials = GetCredentials();
        var objEventLog = new EventLogController();
        var gatewaySettings = new GatewaySetting_ProPay();
        SignupResult_ProPay result = new SignupResult_ProPay();
        using (HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler())))
        {
            HttpRequestMessage request = new HttpRequestMessage();
            HttpResponseMessage response = new HttpResponseMessage();
            response.EnsureSuccessStatusCode();
            client.BaseAddress = new Uri("https://xmltestapi.propay.com/ProPayAPI/signup");
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            string responseBody;
            GatewaySetting_ProPay gatewaySetting = new GatewaySetting_ProPay();
            SignupRequest payerRequest = new SignupRequest();
            HttpContent content = new StringContent(payerRequest.ToString());
            try
            {
                request.Headers.Add("Authorization", credentials);
                request.Headers.Add("accept", "application/json");
                response = await client.PutAsync("https://xmltestapi.propay.com/ProPayAPI/signup", content);
                responseBody = await response.Content.ReadAsStringAsync();
                objEventLog.AddLog("Merchant Onboarding Request Sent", portalSettings, userId,
                    response.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
                Console.WriteLine(responseBody);

            }
            catch (HttpRequestException ex)
            {
                result.Succeeded = false;
                result.Status = ex.Message;
                objEventLog.AddLog("Merchant Onboarding Error!", portalSettings, userId, response.ToString(),
                    EventLogController.EventLogType.ADMIN_ALERT);
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", ex.Message);
            }

            return response.Content.ToString();
        }
    }
public static string GetCredentials()
    {
        GatewaySetting_ProPay gatewaySettings = new GatewaySetting_ProPay();
        var billerAccountId = "mycreds";
        var authToken = "mycreds";
        var encodedCredentials =
            Convert.ToBase64String(Encoding.Default.GetBytes(billerAccountId + ":" + authToken));
        var credentials = string.Format("Basic {0}", encodedCredentials);
        return credentials;
    } 

当我将此方法连接到单击事件时,会收到 HTTP 302 响应,但不会向 API 发送任何内容。需要进行哪些修改才能确保正确传输?

更新 我仍然收到以下回复:

Error code 302 was received from server response.

尽管实施了 AllowAutoRedirect 属性 并将其设置为 true。这是我写的 LoggingHandler class:

public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
    {

    }
    protected async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken, HttpClientHandler handler)
    {
        Console.WriteLine("Request:");
        Console.WriteLine(request.ToString());
        if (request.Content != null)
        {
            Console.WriteLine(await request.Content.ReadAsStringAsync());
        }
        Console.WriteLine();
        handler.AllowAutoRedirect = true;
        HttpResponseMessage response = await base.SendAsync(request, cancellationToken);


        Console.WriteLine("Response:");
        Console.WriteLine(response.ToString());
        if (response.Content != null)
        {
            Console.WriteLine(await response.Content.ReadAsStringAsync());
        }
        Console.WriteLine();

        return response;
    }

这是实现 HttpClientHandler 和相关属性的正确方法吗?

确保 HttpClientHandler 的实例将 AllowAutoRedirect 属性 设置为 true 并由您的 HttpClient 使用。

在为 System.Net.Http 和相关命名空间实施跟踪日志后,日志表明连接已被强制关闭。经过进一步研究,事实证明 .NET Framework 4.5 与更现代的传输层安全 (TLS) 版本不兼容。因此,必须放弃从我们的 DNN 应用程序调用 API 的方法,因为我们正在扩展的源代码以 .NET Framework 4.5 版为目标。