The subject pretty much says it all. I am able to get results when I copy paste the URL from my code to the URL bar of a browser. I'm using the code from http://www.codeproject.com/Tips/397574/Use-Csharp-to-get-JSON-Data-from-the-Web-and-Map-i (the downoload serialized json data function). I also generated the classes as it directed and named the root object MovieDBJSON.
One last tidbit; I'm using the first URL for my test: http://api.themoviedb.org/3/movie/550?api_key=. Yup, the one that supposed to be HELLO World, I'm looking for Mr. Fight Club :).
I saw many people mention the Accept Content-Type header so I added w.Headers.Add("Content-Type", "application/json; charset=utf-8");. This had no effect.
My suspicion is that I have something wrong for the WebClient as I'm getting an exception: "The Remote server returned an error: (406) Not Acceptable."
I've made another discovery which more or less confirms the above suspicion. When I apply the sames methods to the API for RottenTomatoes, everything works perfect. This surely sounds like a data transport error between .NET 4.5 WebClient and theMoveDB.org. I've tried updating the header as stated before and had no luck. I'm curious if anyone has gotten this to work. - NOTE: PHP is my first programming language. I'm just starting to get into .NET so there may be a tweak to the WebClient that I am not aware of.
Thank you in advance for your time and assistance,
Sincerely, Brian D Swan brian.swan@gmail.com
找不到电影或节目?登录并创建它吧。
jameybay 的回复
于 2013 年 05 月 07 日 7:03上午
I'm also working on an app that accesses TheMovieDB and RottenTomatoes. I'm using JSON.NET for serialization and HTTPClient for connection. Your right that the RottenTomatoe api works easily. I had no problems with connection to it but I recieved the 406 (Not Acceptable) error for TheMovieDB. I'm using the Postman Chrome extension for testing my REST calls and had no problems getting the data there, but my app keeps getting this error.
I did try setting the Accept header with the content type and so far it's working.
HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
Travis Bell 的回复
于 2013 年 05 月 07 日 8:48上午
Hey guys,
A 406 (Not Acceptable) means the
Accept
header is not set to "application/json" or "application/json; charset=utf-8". Brian, you tried setting theContent-Type
header. Set yourAccept
header and you'll be good to go.brian.swan@gmail.com 的回复
于 2013 年 05 月 08 日 6:51下午
Thank you all greatly for your help. It makes sense to me now. The website I was trying to get the header from said to set the accept: as Content-Type... I see the error. I have updated my code as
using (var w = new WebClient()) { var jsonData = string.Empty; // attempt to download JSON data as a string w.Headers.Add("Accept", "application/json");
....
This worked perfectly.
Sincerely, Brian