gpt4 book ai didi

c# - 无法初始化类型 'HttpClientHandler'

转载 作者:太空宇宙 更新时间:2023-11-03 21:16:36 24 4
gpt4 key购买 nike

这是我得到的错误:

Cannot initialize type 'HttpClientHandler' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

代码:

using System;
using System.Net;
using System.Net.Http;

namespace Sharepoint_2013_REST_API
{
public class Program
{
public void Main(string[] args)
{
//Init
string baseURL = "";
string uriString = "";
string User = "";
string Password = "";
string Domain = "";

System.Net.Http.HttpClient _Client = new System.Net.Http.HttpClient();
_Client.BaseAddress = new Uri(baseURL);
HttpResponseMessage resp = _Client.GetAsync(uriString).Result;
string respString = resp.Content.ReadAsStringAsync().Result;

_Client = new System.Net.Http.HttpClient(new HttpClientHandler() { new NetworkCredential(User, Password, Domain)});

if (resp.StatusCode != HttpStatusCode.OK)
{
Console.Write("Status code: " + resp.StatusCode);
Console.ReadLine();
}
}


}
}

希望你能帮我解决这个错误?

最佳答案

您忘记了将 NewworkCredential 实例分配给对象初始值设定项中的 Credentials 属性。

new HttpClient(new HttpClientHandler() {{Credentials = new System.Net.NetworkCredential(user, pwd, domain)});

关于c# - 无法初始化类型 'HttpClientHandler',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33827017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com