How to initialize WebClient using object initializers?

后端 未结 2 539
别跟我提以往
别跟我提以往 2021-01-19 13:27

I have a WebClient like this:

WebClient _webClient = new WebClient
{
    UseDefaultCredentials = true,
    Encoding = System.Text.Encoding.UTF8,         


        
相关标签:
2条回答
  • 2021-01-19 14:14

    I think you can also do it like this (without the : in the header):

    private static WebClient _doClient = new WebClient
    {
        BaseAddress = "https://api.digitalocean.com/v2/domains/example.com/",
        Headers = new WebHeaderCollection { { "Authorization", "Bearer " + _digitalOceanApiKey } }
    };
    
    0 讨论(0)
  • 2021-01-19 14:15

    You can do it like this. You have to split the property and its value by a colon (:):

    WebClient _webClient = new WebClient
        {
            UseDefaultCredentials = true,
            Encoding = System.Text.Encoding.UTF8,
            Headers = new WebHeaderCollection
            {
                "user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
            }
        };
    
    0 讨论(0)
提交回复
热议问题