0

代码非常简单:

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 崩溃了。

4

1 回答 1

0

这是请求失败时 HttpWebRequest 的 Silverlight 浏览器实现中的一个错误(请注意堆栈中的 HandleAsyncException)。我们在 Microsoft.Net.Http 的较新版本中提供了一个解决方法,该版本应该很快就会发布。

现在您可以尝试在 SL 中使用不返回空流的客户端堆栈。 http://msdn.microsoft.com/en-us/library/dd920295(v=VS.95).aspx

于 2013-04-25T02:26:50.860 回答