0
votes

i want to get lyrics using web api but it has an error of 'System.Net.Http.HttpClient' does not contain a definition for 'DownloadData' and no extension method 'DownloadData' accepting a first argument of type 'System.Net.Http.HttpClient' could be found (are you missing a using directive or an assembly reference?)

here my code

internal static class LyricsFetcher
{
    internal static String GetLyrics(String Artist, String Title)
    {
        byte[] responseData;

        string URL;

        URL = "http://api.metrolyrics.com/v1/search/lyrics/?find=" + Artist + "%20" + Title + "&X-API-KEY=1234567890123456789012345678901234567890";

        HttpClient wClient = new HttpClient();
        responseData = wClient.DownloadData(URL); // error
        UTF8Encoding utf8 = new UTF8Encoding();
        String Lyrics = utf8.GetString(responseData,0,responseData.Length);
        return Lyrics;
    }
}
2
What does a compiler error(!) have to do with what site you're connecting to?PMF

2 Answers

3
votes

Rather than using DownloadData (which is not available in HttpClient), you should use GetAsync or GetStreamAsync. The advantage of HttpClient is you can use async methods, so you should keep using it. As a bonus it is also available in PCL so that ultimately you can use your component across multiple platforms.

    internal static async Task<String> GetLyrics(String Artist, String Title)
    {
        byte[] responseData;

        string URL;

        URL = "http://api.metrolyrics.com/v1/search/lyrics/?find=" + Artist + "%20" + Title + "&X-API-KEY=1234567890123456789012345678901234567890";

        HttpClient wClient = new HttpClient();
        responseData = await wClient.GetByteArrayAsync(URL); // success!
        UTF8Encoding utf8 = new UTF8Encoding();
        String Lyrics = utf8.GetString(responseData, 0, responseData.Length);
        return Lyrics;
    }
0
votes

HttpClient doesn't have a DownloadData method. I believe you might have meant to use WebClient, which has a DownloadData method.

WebClient wClient = new WebClient();
responseData = wClient.DownloadData(URL);