代码非常简单:
var http = new HttpClient();
var uri = new Uri(Application.Current.Host.Source, "/api/query");
var postContext = new StringContent("test");
var response = await http.PostAsync(uri, postContext);
错误是:对象空引用
at System.Net.Http.DelegatingStream.get_CanSeek()
at System.Net.Http.StreamContent..ctor(Stream content, Int32 bufferSize)
at System.Net.Http.StreamContent..ctor(Stream content)
at System.Net.Http.HttpClientHandler.CreateResponseMessage(HttpWebResponse webResponse, HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.TryGetExceptionResponse(WebException webException, HttpRequestMessage requestMessage, HttpResponseMessage& httpResponseMessage)
at System.Net.Http.HttpClientHandler.HandleAsyncException(RequestState state, Exception e)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass19.<InvokeGetResponseCallback>b__17(Object state2)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
GetAsync 工作得很好。但是 PostAsync 崩溃了。