我想在youtube搜索结果中获取元素和标签,下面是windows phone应用程序的代码 .
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:远程服务器返回错误:NotFound . ---> System.Net.WebException:远程服务器返回错误:NotFound . 在System.Net.Browser.ClientHttpWebRequest上的System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) . <> c__DisplayClasse.b__d(Object sendState)在System.Net.Browser.AsyncHelper . <> c__DisplayClass1.b__0(Object sendState) ---内部异常堆栈跟踪的结束---在System.Net.WebClient的System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)中 . System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)}中的GetWebResponse(WebRequest请求,IAsyncResult结果)
有谁知道为什么服务器返回404没找到?
注意:关键字只能是"ali, veli, kadir"等字符串 .
1 回答
NotFound
是WebClient的多种错误回应 . 最常见的两种情况是错误的服务器证书(不应该是你的情况,Youtube有一个有效的证书)
您的模拟器或设备中没有Internet连接 .
我几乎可以肯定你的情况是第二个,检查你的互联网连接 .