我正在将我的 Windows 8.1 应用程序移植到 Windows 10 UWP,但PostAsync
现在调用会引发异常。
这个确切的代码在面向 8.1 时完美运行,但是当我面向 Windows 10 UWP 时,它会引发以下异常:
This IRandomAccessStream does not support the GetInputStreamAt method because it requires cloning and this stream does not support cloning.
代码
public async void TestPost()
{
var parameters = GetParameters();
var formattedData = new FormUrlEncodedContent(parameters);
using (var clientHandler = new HttpClientHandler { Credentials = GetCredentials() })
{
using (var httpClient = new HttpClient(clientHandler))
{
var response = await httpClient.PostAsync(postUrl, formattedData);
}
}
}
private Dictionary<string, string> GetParameters()
{
var parameters = new Dictionary<string, string>();
parameters["grant_type"] = "url";
parameters["device_id"] = "unique key";
parameters["redirect_uri"] = "redirect url";
return parameters;
}
public static NetworkCredential GetCredentials()
{
return new NetworkCredential("<secret key>", "");
}
堆栈跟踪
at System.IO.NetFxToWinRtStreamAdapter.ThrowCloningNotSuported(String methodName)
at System.IO.NetFxToWinRtStreamAdapter.GetInputStreamAt(UInt64 position)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpHandlerToFilter.<SendAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClientHandler.<SendAsync>d__1.MoveNext()