Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中,我可以在Restsharp中设置一些httpclienthandler属性吗?_C#_Rest_Restsharp_Dotnet Httpclient - Fatal编程技术网

在C#中,我可以在Restsharp中设置一些httpclienthandler属性吗?

在C#中,我可以在Restsharp中设置一些httpclienthandler属性吗?,c#,rest,restsharp,dotnet-httpclient,C#,Rest,Restsharp,Dotnet Httpclient,我用C#编写了以下代码,它使用了HTTPClient,我正在尝试迁移到以利用这些优秀的Derserialization代码 这是我目前的代码: var httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, AllowAutoRedirect = false }); var response = ht

我用C#编写了以下代码,它使用了HTTPClient,我正在尝试迁移到以利用这些优秀的Derserialization代码

这是我目前的代码:

 var httpClient = new HttpClient(new HttpClientHandler()
        {
            UseDefaultCredentials = true,
            AllowAutoRedirect = false
        });

 var response = httpClient.GetStringAsync(myUrl).Result;
以下是使用restsharp的等效代码:

 _client = new RestClient { BaseUrl =new Uri(myUrl) };
 var request = new RestRequest { Method = method, Resource = "/project", RequestFormat = DataFormat.Json };
 var response = _client.Execute(request);
但我不知道怎么设置

 UseDefaultCredentials = true


在尖锐的一面。是否支持此功能?

如果要使用基本HTTP身份验证,您需要为RestSharp提供如下基本身份验证信息

 _client = new RestClient { BaseUrl =new Uri(myUrl) };
_client.Authenticator = new HttpBasicAuthenticator("<username>", "<password>");

如果有帮助,请参阅更新的答案。为什么不将反序列化程序移植到HttpClient上使用呢?谢谢。关于设置:AllowAutoRedirect有什么想法吗
 _client = new RestClient { BaseUrl =new Uri(myUrl) };
_client.Authenticator = new HttpBasicAuthenticator("<username>", "<password>");
    const Method httpMethod = Method.GET;
    string BASE_URL = "http://localhost:8080/";

    var client = new RestClient(BASE_URL);
    // This property internally sets the AllowAutoRedirect of Http webrequest
    client.FollowRedirects = true;
    // Optionally you can also add the max redirects 
    client.MaxRedirects = 2;

    var request = new RestRequest(httpMethod)
    {
        UseDefaultCredentials = true
    };

    client.Execute(request);