我尝试使用POST
方法
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();
}
}
但是当我运行代码时,它会给我错误
WebException: The request requires buffering data to succeed HttpClient
。
我不想使用WebRequest
或WebClient
发出请求,我只想使用HttpClient
。
尝试发送head request
时,question已经存在但发生相同异常的情况说明问题仅发生在Xamarin
个项目上,但问题未得到解答然而。刚刚提到他使用WebRequest
来解决问题。
Xamarin.Forms
上此代码的错误是什么?如何使用HttpClient
来解决?
{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 }
答案 0 :(得分:2)
基于HttpWebRequest的Mono源代码,看起来服务器正在发出重定向,并且HttpWebRequest(由HttpClient负责使用)不处理它。
我不确定使用的是什么版本的Mono源,但这似乎是一个可能的候选者: https://github.com/mono/mono/blob/cc3f4c60379c3839dd4259e171bb4539d21f2157/mcs/class/System/System.Net/HttpWebRequest.cs
该版本有&#34; throw&#34;在HttpWebRequest.EndGetResponse的第1005行,它匹配您正在查看的异常堆栈。异常中的字符串,&#34;请求需要缓冲数据才能成功&#34;来自HttpWebRequest.Redirect。
下一步是使用Fiddler(或类似的)来复制Xamarin客户端试图查看从服务器获得的响应的确切POST消息。
答案 1 :(得分:2)
不幸的是
var response = await client.PostAsyn(url, content);
在完成之前下载整个响应,因此它不使用任何缓冲。唯一的选择是使用,
var request = new HttpMessageRequest(url);
request.Content = content;
var response = await client.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead);
下载标题后, HttpCompletionOption.ResponseHeadersRead
选项会使HttpClient
从SendAsync
返回。你可以等待阅读内容。
答案 2 :(得分:2)
对于您在提出头部请求时链接的相同问题,请参阅以下链接,其中xamarin论坛中提供了其中一个解决方案,他们在向Google网址发出POST请求(200)时遇到了同样的问题。
这种情况下的解决方案是将URL更改为实际页面而不是重定向。
希望它有所帮助。
答案 3 :(得分:1)