How to initialize WebClient using object initializers?

后端 未结 2 538
别跟我提以往
别跟我提以往 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: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;)"
            }
        };
    

提交回复
热议问题