![](/img/trans.png)
[英]HttpWebRequest GET ProtocolViolationException WP7
[英]How to get exception/ error that raise by HttpWebrequest for Wp7
有時服務器關閉,服務器上的文件丟失等問題。 因此,我想在使用Dispatcher線程更新UI上的內容時捕獲或捕獲HttpWebRequest拋出的異常。
下面的代碼無法捕獲錯誤並顯示在MessageBox.show()中。 誰能告訴我我需要做什么? 謝謝
HttpWebRequest webReq;
HttpWebResponse webResp;
public void GetInfo(string Url)
{
webReq = (HttpWebRequest)HttpWebRequest.Create(Url);
try
{
webReq.BeginGetResponse(OnGetBuffer, this);
}
catch (Exception e)
{
}
}
public void OnGetBuffer(IAsyncResult asr)
{
webResp = (HttpWebResponse)webReq.EndGetResponse(asr);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Stream streamResult = webResp.GetResponseStream();
try
{
}
catch (Exception)
{
}
});
}
在.EndGetResponse()調用周圍放置一個try / catch。 我相信這是拋出異常的地方。
首先,我希望你不打算捕獲所有異常並忽略它們。 您將忽略與網絡連接失敗無關的異常。
其次,您需要將try / catch放在可能引發異常的代碼周圍:
public void OnGetBuffer(IAsyncResult asr)
{
HttpWebResponse webResp;
try
{
webResp = (HttpWebResponse)webReq.EndGetResponse(asr);
}
Catch (WebException ex)
{
// Do something to decide whether to retry, then retry or else
throw; // Re-throw if you're not going to handle the exception
}
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
using (Stream streamResult = webResp.GetResponseStream())
{
// Do something with the stream
}
});
}
嘗試使用WebClient
對象。 然后在完成的事件處理程序中,錯誤將作為e.Error
返回
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.