【问题标题】:NullReferenceException reading from an asynchronous HttpWebRequest streamNullReferenceException 从异步 HttpWebRequest 流中读取
【发布时间】:2012-01-29 20:51:25
【问题描述】:

我正在为 Windows Phone 7 编写一个应用程序。这个应用程序首先发送数据,然后通过 HttpWebRequest 从服务器接收数据。大多数情况下它工作正常,但有时,在正确接收部分数据后,我在 Stream.Read() 函数中得到 NullReferenceException。

当用户按下按钮时,通信开始。然后我创建 HttpWebRequest:

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
}

最后,这是我在读取流数据时遇到异常的代码:

int iBytesLeidos;
byte[] byteArrayUTF8 = new byte[8];

iBytesLeidos = streamResponse.BaseStream.Read(byteArrayUTF8, 0, 8); //NullReferenceException!!! -Server always send 8 bytes here-

当应用程序启动时,我创建了一个后台线程,该线程经常向服务器发送信息。后台和手动通信可以同时运行。这会是个问题吗?

谢谢。

【问题讨论】:

    标签: c# windows-phone-7 httpwebrequest nullreferenceexception getresponsestream


    【解决方案1】:

    如果streamResponse 是全局变量,则在从另一个线程访问的情况下可能会导致问题。将您的Stream 作为参数传递给Recv

    using (StreamReader streamResponse = new StreamReader(response.GetResponseStream() ) )
    {
      this.bSyncOK = Recv(streamResponse); //This is my generic method to receive the data
      streamResponse.Close();
    }
    

    【讨论】:

      【解决方案2】:

      你的streamResponse 在后面的 sn-p 中声明在哪里?它与 3d sn-p 中的对象是否相同?也许您只是使用另一个变量,而不是实际的流。

      【讨论】:

      • 是的,所有代码都属于同一个类。这是“streamResponse”的声明-->“private static StreamReader streamResponse;”
      • 能否检查streamResponse.BaseStream 是否为空?
      • 好吧,我在桌面上试过你的代码——它可以工作。您能否查看我的版本 bit.ly/utT5k3 并确认您的版本是否相同?也可能存在竞争条件——我没有注意到你在哪里重置了你的事件。我在我的解决方案中添加了并行版本。
      【解决方案3】:

      在第二个 sn-p 中,尝试删除“postStream.Close();”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-11
        相关资源
        最近更新 更多