C# 从异步HttpWebRequest流读取NullReferenceException
我正在为WindowsPhone7编写一个应用程序。该应用程序首先通过HttpWebRequest发送数据,然后从服务器接收数据。大多数情况下,它工作正常,但有时,在正确接收部分数据后,我在Stream.Read()函数中得到一个NullReferenceException 当用户按下按钮时,通信开始。然后创建HttpWebRequest:C# 从异步HttpWebRequest流读取NullReferenceException,c#,windows-phone-7,httpwebrequest,nullreferenceexception,getresponsestream,C#,Windows Phone 7,Httpwebrequest,Nullreferenceexception,Getresponsestream,我正在为WindowsPhone7编写一个应用程序。该应用程序首先通过HttpWebRequest发送数据,然后从服务器接收数据。大多数情况下,它工作正常,但有时,在正确接收部分数据后,我在Stream.Read()函数中得到一个NullReferenceException 当用户按下按钮时,通信开始。然后创建HttpWebRequest: HttpWebRequest request = (HttpWebRequest) WebRequest.Create(sUri); request.Met
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(sUri);
request.Method = "POST";
request.BeginGetRequestStream(GetRequestStreamCallback, request);
请求回调方法:
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
postStream = request.EndGetRequestStream(asynchronousResult);
this.bSyncOK = Send(); //This is my method to send data to the server
postStream.Close();
if (this.bSyncOK)
request.BeginGetResponse(GetResponseCallback, request);
else
manualEventWait.Set(); //This ManualResetEvent notify a thread the end of the communication, then a progressbar must be hidden
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult) )
{
using (streamResponse = new StreamReader(response.GetResponseStream() ) )
{
this.bSyncOK = Recv(); //This is my generic method to receive the data
streamResponse.Close();
}
response.Close();
}
manualEventWait.Set(); //This ManualResetEvent notify a thread the end of the communication, then a progressbar must be hidden
}
响应回调方法:
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
postStream = request.EndGetRequestStream(asynchronousResult);
this.bSyncOK = Send(); //This is my method to send data to the server
postStream.Close();
if (this.bSyncOK)
request.BeginGetResponse(GetResponseCallback, request);
else
manualEventWait.Set(); //This ManualResetEvent notify a thread the end of the communication, then a progressbar must be hidden
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult) )
{
using (streamResponse = new StreamReader(response.GetResponseStream() ) )
{
this.bSyncOK = Recv(); //This is my generic method to receive the data
streamResponse.Close();
}
response.Close();
}
manualEventWait.Set(); //This ManualResetEvent notify a thread the end of the communication, then a progressbar must be hidden
}
最后,这是读取流数据时出现异常的代码:
int iBytesLeidos;
byte[] byteArrayUTF8 = new byte[8];
iBytesLeidos = streamResponse.BaseStream.Read(byteArrayUTF8, 0, 8); //NullReferenceException!!! -Server always send 8 bytes here-
当应用程序启动时,我创建一个后台线程,该线程经常向服务器发送信息。后台通信和手动通信可以同时运行。这可能是个问题吗
谢谢。您的
流响应在后一段代码中声明在哪里?它是否与3d代码段中的对象相同?可能您只是使用了另一个变量,而不是实际的流。如果streamResponse
是全局变量,则在从另一个线程访问时可能会导致问题。将流
作为参数传递给Recv
using (StreamReader streamResponse = new StreamReader(response.GetResponseStream() ) )
{
this.bSyncOK = Recv(streamResponse); //This is my generic method to receive the data
streamResponse.Close();
}
在第二个代码段中,尝试删除“postStream.Close();” 是的,所有代码都属于同一类。这是“streamResponse”->“private static StreamReader streamResponse”的声明;您可以检查streamResponse
或.BaseStream
是否为空吗?好的,我在桌面上尝试了您的代码——它可以工作。你能检查一下我的版本并确认你的版本看起来是一样的吗?还有可能是比赛条件——我没有注意到你在哪里重置了你的赛事。我已将并行版本添加到我的解决方案中。