问题描述
我正在编写一个后台音频代理,它播放来自在线流的音乐,并定期检查曲目名称和艺术家的更新.我正在尝试使用 HttpWebRequest 对象来获取名称和艺术家,但是每当我调用 HttpWebResponse trackResponse = (HttpWebResponse)trackRequest.EndGetResponse(result);
时,都会抛出以下错误.
I'm writing a background audio agent that plays music from an online stream and also periodically checks for updates in the track name and artist. I'm attempting to use an HttpWebRequest object to get the name and artist, but whenever I call HttpWebResponse trackResponse = (HttpWebResponse)trackRequest.EndGetResponse(result);
the error below is thrown.
System.Net.WebException"类型的第一次机会异常发生在 System.Windows.dll 中
WebException 的堆栈跟踪如下:
The stack trace for the WebException is the following:
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 对象,我发现:
Digging further into the trackRequest object, I find this:
ResponseStatusCode = 'trackRequest.ResponseStatusCode' 抛出了类型为 '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 函数每 20 秒由 Timer 调用一次.
Here is the code I am using. The TrackTimerTick function is called every 20 seconds by a Timer.
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();
}
谁能帮我解决这个问题?提前致谢.
Can anyone help me fix this problem? Thank you in advance.
推荐答案
就我而言,我不得不查看我的项目错误日志,其中通常会抛出异常并打印消息 Unable to create an SSL/TLS安全通道.在 System.Net.HttpWebRequest.GetResponse()
中,为了解决这个问题,我添加了 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 |SecurityProtocolType.Tls |SecurityProtocolType.Tls11 |SecurityProtocolType.Tls12;
在我的请求之前,但你可以把它放在你的启动方法或类上执行.我认为其他人认为你可以做的是在 try 和 catch 异常中添加响应
In my case I had to look at my project error log where normally exceptions are thrown and it was printing the message Unable to create an SSL/TLS secure channel. in System.Net.HttpWebRequest.GetResponse()
and to solve this I added ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
before my request, but you can put it to be executed on your starter method or class. I think other think that you can do is to add the response in try and catch exception
try
{
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Response response = new JavaScriptSerializer().Deserialize<Response>(result);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
这里是你的响应类
public class Response
{
public string value_02 { get; set; }
public string value_03 { get; set; }
public string value_04 { get; set; }
public string value_05 { get; set; }
public string value_05 { get; set; }
}
事实对我来说是例外
Exception thrown: 'System.Security.Authentication.AuthenticationException' in System.dll
Exception thrown: 'System.Security.Authentication.AuthenticationException' in System.dll
Exception thrown: 'System.Security.Authentication.AuthenticationException' in System.dll
Exception thrown: 'System.Security.Authentication.AuthenticationException' in System.dll
Exception thrown: 'System.Security.Authentication.AuthenticationException' in System.dll
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.NullReferenceException'
而且我已经添加来解决我的问题
And I have added to solve my problem
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
解决了我的问题
这篇关于HttpWebRequest 返回 null ResponseStatusCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!