iTunes Search API get 0 results at c# programm but 1 result at Chrome browser

188 Views Asked by At

I created a C# programm which searches movie details with the title of the movie. I create the link:

        string link = "https://itunes.apple.com/search?";

        link += "term=";

        string cTitle = Titel.Replace(" ", "+");
        link += cTitle;

        link += "&country=DE";
        link += "&media=movie";
        link += "&entity=movie";
        link += "&attribute=movieTerm";
        link += "&limit=1";

with the movie title "Bus 657" that would be the following link:

https://itunes.apple.com/search?term=Bus+657&country=DE&media=movie&entity=movie&attribute=movieTerm&limit=1

If I open that link I get a txt file with the result I need. But if I get that in c# with the following code:

WebClient client = new WebClient();
var json = client.DownloadString(link);
Thread.Sleep(3100);

I get 0 results. Can someone help me to fix that?

I have a lot movie titles which title are named by the original iTunes title of the store at germany.

Thx :)

2

There are 2 best solutions below

4
ザヘド On BEST ANSWER

Add the User Agent to the request header and try.

WebClient client = new WebClient();
client.Headers.Add("user-agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
var json = client.DownloadString(link);

I received "resultCount":1

3
AudioBubble On

Your link is wrong.

You have:

                                        BZZZZT - should be %20
                                        ↓
https://itunes.apple.com/search?term=Bus+657&country=DE&media=movie&entity=movie&attribute=movieTerm&limit=1

Your movie title is "Bus 657". That space should be encoded as %20, not a +. Your link should be:

https://itunes.apple.com/search?term=Bus%20657&country=DE&media=movie&entity=movie&attribute=movieTerm&limit=1

With the corrected link, you get results. You should be using HttpServerUtility.UrlEncode or some other function to properly URL encode your URL. Or build your URL using the UriBuilder class.