具有身份验证的C#Http请求

时间:2020-07-30 04:46:43

标签: c# http authentication httprequest http-get

我需要您的帮助,所有其他问题对我来说都没有解决。

我想用C#编写一个简单的Http请求。在那之后它应该是一个http get。但是我要请求的API需要身份验证。

我可以与服务器连接,但是我不知道如何编程 对此进行身份验证。

用户名和密码均为“测试”

public static class HttpGet
{
    static void Main()
    {
        RunAsync().Wait();
    }

    public static HttpClient ApiCLient { get; set; }
    
    static async Task RunAsync()
    {
        string username = "test";
        string password = "test";

        using (var ApiCLient = new HttpClient())
        {
            ApiCLient.BaseAddress = new Uri("https://localhost:44325/");
            ApiCLient.DefaultRequestHeaders.Accept.Clear();
            ApiCLient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await ApiCLient.GetAsync("weatherforecast");

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("ddd");
            }
        }
    }
}

我希望有人能帮助我。我尝试的每种解决方案都不适合我...

谢谢:)

(我是ASP.NET的新手)

0 个答案:

没有答案