2015-04-26 82 views
1

我想用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」或其他字符串。

回答

0

NotFound是WebClient的響應多種錯誤。兩種最常見的情況是

  • 壞的服務器證書(不應該是你的情況下,YouTube已經有效證書)
  • 在模擬器或設備沒有互聯網連接。

我幾乎可以肯定你的情況是第二個,檢查你的互聯網連接。

+0

檢查,它可以從ie內部連接youtube。它在「Windows窗體應用程序」上運行,它現在也不運行。 –

相關問題