1

I wanted to get elements and tags inside of youtube search results with below code for windows phone application.

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);
        }
    }
}

and get exception below:

{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)}

does anybody know why server return 404 not found?
note: Keyword can only be string like "ali, veli, kadir" or something else.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

0

NotFound is WebClient`s response many kinds of errors. The two most common cases are

  • bad server certificate (should not be your case, Youtube has a valid certificate)
  • no Internet connection in your emulator or device.

I almost sure your case is the second one, check your Internet connection.

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118