尝试获取WebResponse的回调中的异常

时间:2013-03-20 17:35:15

标签: httpwebrequest silverlight-5.0 httpwebresponse

我正在使用HttpWebRequest来传输文件。工作流程如下所示:

webRequest.BeginGetRequestStream(new AsyncCallback(WriteToStreamCallback), webRequest);

在WriteStreamToCallback中:

HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;  
Stream requestStream = webRequest.EndGetRequestStream(asynchronousResult);  

将字节写入requestStream,然后调用:

webRequest.BeginGetResponse(new AsyncCallback(ReadHttpResponseCallback), webRequest);  

ReadHttpResponseCallback中发生异常:

HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;  
// The next line throws a WebException
HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

例外是:

  

类型' System.Net.WebException'的第一次机会异常发生在   System.Windows.dll中
  远程服务器返回错误:NotFound。

应用可以从中恢复吗?可能是什么原因?这是间歇性的;这是时间问题吗? 感谢您的任何见解...

更新

private void ReadHttpResponseCallback(IAsyncResult asynchronousResult)
{
    try
    {
        HttpWebRequest webRequest = (HttpWebRequest) asynchronousResult.AsyncState;
        // The next line throws the exception
        HttpWebResponse webResponse = (HttpWebResponse) webRequest.EndGetResponse(asynchronousResult);

        /* SNIP */
    }
    catch (WebException webEx)
    {
        System.Diagnostics.Debug.WriteLine(string.Format("      {0} {1}", "ReadHttpResponseCallback - WebException", webEx.Message));
    }
    catch (Exception xx)
    {
        System.Diagnostics.Debug.WriteLine(string.Format("{0}  {1}", "ReadHttpResponseCallback", xx.Message));
    }
}

1 个答案:

答案 0 :(得分:0)