The request requires buffering data to succeed HttpClient

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

I'm trying to send a dictionary content to a server with POST method

public async Task<T> postConnection(string GETParam, Dictionary<string, string> values, bool isRegistration = false) {     HttpResponseMessage response;     string responseString;     try     {          using (var client = new HttpClient() { MaxResponseContentBufferSize = 256000 })          {             client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tocken);             var content = new FormUrlEncodedContent(values);             response = await client.PostAsync(url, content);             responseString = await response.Content.ReadAsStringAsync();         }     }

But when I run the code it gives me the error
WebException: The request requires buffering data to succeed HttpClient.

I don't want to make the request using WebRequest or WebClient, I want to use only HttpClient.

A question exists already with the same exception occurring when trying to send a head request says that the problem is occurred only on Xamarin projects, but the question wasn't answered yet. It's just mentioned that he solved the problem by using WebRequest.

What is the error with this code on Xamarin.Forms? and How could it be solved using HttpClient?

edit

Full exception

{System.Net.WebException: The request requires buffering data to succeed.   at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1005    at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (IAsyncResult iar, System.Func`2 endFunction, System.Action`1 endAction, System.Threading.Tasks.Task`1 promise, Boolean requiresSynchronization) [0x00014] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550  --- End of stack trace from previous location where exception was thrown ---   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535    at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.MoveNext () [0x003d6] in /Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:372  --- End of stack trace from previous location where exception was thrown ---   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535    at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x000a9] in /Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276  --- End of stack trace from previous location where exception was thrown ---   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128    at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357    at HuraApp.API.Connection`1+<postConnection>d__1[T].MoveNext () [0x0011c] in C:\Users\Nullsky\documents\visual studio 2015\Projects\HuraApp\HuraApp\HuraApp\API\Connection.cs:43 }

回答1:

For the same question you have linked in making a head request , please see the below link where the solution is given one of them in xamarin forums, who had the same issue in making a POST request (200) to Google url.

The resolution in that case was changing the URL to an actual page and not a redirect.

Here 's the link Solution for the issue

Hope it helps.



回答2:

Based on the Mono source code for HttpWebRequest, it looks like the server is issuing a redirect, and HttpWebRequest (used under the covers by HttpClient) is not handling that.

I'm not exactly sure what version of the Mono source is being used, but this looks like a likely candidate: https://github.com/mono/mono/blob/cc3f4c60379c3839dd4259e171bb4539d21f2157/mcs/class/System/System.Net/HttpWebRequest.cs

That version has a "throw" at line 1005 in HttpWebRequest.EndGetResponse, which matches the exception stack you're seeing. The string in the exception, "The request requires buffering data to succeed" comes from HttpWebRequest.Redirect.

The next step would be to use Fiddler (or similar) to replicate the exact POST message the Xamarin client is attempting to see the response it gets from the server.



回答3:

Unfortunately

var response = await client.PostAsyn(url, content);

Downloads entire response before finishing, so it does not use any buffering. The only alternative is to use,

var request = new HttpMessageRequest(url); request.Content = content; var response = await client.SendAsync(request,        HttpCompletionOption.ResponseHeadersRead);

HttpCompletionOption.ResponseHeadersRead option makes HttpClient return from SendAsync just after downloading headers. And you can await on reading the content.



回答4:

Is this happening on iOS and/or Android?

I would try changing the HttpClient handler to be the native handler. This solves a lot of problems ranging a lot of topics (TLS is a big one). For iOS, use the NSUrlSession handler and for Android, try the AndroidClientHandler. You should be able to use HttpClient for everything with Xamarin.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!