我正在编写一个背景音频代理,可以播放来自在线流的音乐,还会定期检查曲目名称和艺术家的更新。我正在尝试使用HttpWebRequest对象来获取名称和艺术家,但每当我调用HttpWebResponse trackResponse = (HttpWebResponse)trackRequest.EndGetResponse(result);
时,都会抛出以下错误。
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll
WebException的堆栈跟踪如下:
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at AudioPlaybackAgent.AudioPlayer.TrackCallback(IAsyncResult result)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
进一步深入了解trackRequest对象,我发现:
ResponseStatusCode = 'trackRequest.ResponseStatusCode' threw an exception of type 'System.NullReferenceException'
进一步说,我发现了这个:
at System.Net.HttpWebRequest.get_ResponseStatusCode()
at AudioPlaybackAgent.AudioPlayer.TrackCallback(IAsyncResult result)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
这是我正在使用的代码。 TrackTimerTick函数由Timer定时器每20秒调用一次。
public static void TrackTimerTick(object state) {
try {
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing) {
// Create a HttpWebrequest object to the desired URL.
HttpWebRequest trackRequest = (HttpWebRequest)HttpWebRequest.Create("<track/artist url");
// Start the asynchronous request.
IAsyncResult result = (IAsyncResult)trackRequest.BeginGetResponse(new AsyncCallback(TrackCallback), trackRequest);
}
} catch (WebException e) {
Debug.WriteLine(e.Message);
} catch (Exception e) {
Debug.WriteLine(e.Message);
}
}
public static void TrackCallback(IAsyncResult result) {
// State of request is asynchronous.
HttpWebRequest trackRequest = (HttpWebRequest)result.AsyncState;
HttpWebResponse trackResponse = (HttpWebResponse)trackRequest.EndGetResponse(result); // WebException thrown here
using (StreamReader httpwebStreamReader = new StreamReader(trackResponse.GetResponseStream())) {
string results = httpwebStreamReader.ReadToEnd();
XDocument trackXml = XDocument.Load(results);
string title = (from t in trackXml.Descendants("channel") select t.Element("title").Value).First<string>();
string artist = (from t in trackXml.Descendants("channel") select t.Element("artist").Value).First<string>();
if (BackgroundAudioPlayer.Instance.Track != null) {
AudioTrack track = BackgroundAudioPlayer.Instance.Track;
track.BeginEdit();
track.Title = title;
track.Artist = artist;
track.EndEdit();
}
}
trackResponse.Close();
}
任何人都可以帮我解决这个问题吗?提前谢谢。
答案 0 :(得分:0)
问题是您在响应到达之前调用NotifyComplete()。我不完全了解会发生什么,但你发起请求,你调用NotifyComplete,操作系统冻结了代理的进程,然后下一次代理唤醒时,WebClient立即抛出异常,可能是设计。
因此解决方案是在得到响应之前不要调用NotifyComplete。