简单Flurl GET引发ProtocolViolationException:无法发送具有此动词类型的content-body

时间:2019-10-11 23:16:10

标签: http flurl

我有一个简单的HTTP客户端,该客户端使用Flurl.Http向API发出请求。 我在客户端的构造函数中进行了一些配置。

HttpConfiguration.ConfigureDomain(postBaseUrl);
HttpConfiguration.ConfigureDomain(getBaseUrl);

ConfigureDomain方法包含:

        FlurlHttp.ConfigureClient(url, cli =>
            cli.Settings.HttpClientFactory = new UseDefaultCredentialsClientFactory());

        FlurlHttp.ConfigureClient(url, cli =>
            cli.WithHeader("Content-Type", "application/json")
               .WithHeader("Accept", "application/json"));

        FlurlHttp.Configure(settings => {
            var jsonSettings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                DefaultValueHandling = DefaultValueHandling.Ignore
            };
            settings.JsonSerializer = new NewtonsoftJsonSerializer(jsonSettings);
        });

        FlurlHttp.ConfigureClient(url, cli =>
            cli.Configure(settings =>
            {
                settings.BeforeCall = call => Log.Info($"Received response {call.Response.StatusCode} with payload {call.Response.Content.ReadAsStringAsync().Result}");
                settings.AfterCall = call => Log.Info($"Received response {call.Response.StatusCode} with payload {call.Response.Content.ReadAsStringAsync().Result}");
                settings.OnError = call => Log.Error($"Call failed with exception: {call.Exception} \n\r");
            }));

最后,我正在创建URL字符串并发出GET请求,如下所示:

getBaseUrl.AppendPathSegments("Segment1", "Segment2").GetJsonAsync<Dto>().Result;

最终会引发以下异常:

    Call failed with exception: System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
   at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
   at System.Net.HttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
   at System.Net.Http.HttpClientHandler.StartGettingRequestStream(RequestState state)
   at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload(RequestState state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Flurl.Http.FlurlRequest.<SendAsync>d__19.MoveNext() 

POST请求可以在相同的设置下并使用PostJsonAsyn(body)正常工作。

0 个答案:

没有答案