WebException:远程服务器返回错误:NotFound Windows Phone Youtube Html
我想用windows phone应用程序的以下代码获取YouTube内搜索结果中的元素和标签。下面WebException:远程服务器返回错误:NotFound Windows Phone Youtube Html
public partial class Page1 : PhoneApplicationPage
{
string keyword;
public Page1()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
NavigationContext.QueryString.TryGetValue("parameter", out keyword);
LoadResults();
}
public void LoadResults()
{
WebClient codeSampleReq = new WebClient();
codeSampleReq.DownloadStringCompleted += codeSampleReq_DownloadStringCompleted;
codeSampleReq.DownloadStringAsync(new Uri("https://www.youtube.com/results?search_query=" + keyword));
}
void codeSampleReq_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionFixNestedTags = true;
htmlDoc.LoadHtml(e.Result);
HtmlNode divContainer = htmlDoc.GetElementbyId("a");
if (divContainer != null)
{
HtmlNodeCollection nodes = divContainer.SelectNodes("a");
foreach (HtmlNode trNode in nodes)
{
}
}
}
catch (Exception ex)
{
MessageBox.Show("Unable to download" + ex.Message);
}
}
}
,并得到例外:
{System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.b__d(Object sendState) at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.b__0(Object sendState) --- End of inner exception stack trace --- at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)}
有谁知道为什么服务器返回404没有发现?
注意:关键字只能是“ali,veli,kadir”或其他字符串。
NotFound
是WebClient的响应多种错误。两种最常见的情况是
- 坏的服务器证书(不应该是你的情况下,YouTube已经有效证书)
- 在模拟器或设备没有互联网连接。
我几乎可以肯定你的情况是第二个,检查你的互联网连接。
检查,它可以从ie内部连接youtube。它在“Windows窗体应用程序”上运行,它现在也不运行。 –