Reputation: 1854
So I'm attempting to connect to a web service to retrieve JSON and display it to my View. However any time I run the code I receive an error saying:
"Unable to connect to the remote server."
When I navigate to the web service URL in a browser it displays a JSON string just the way it's supposed to.
My controller class Tweets.cs:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TwitterClientMVC.Controllers
{
public class Tweets
{
public Tweet[] results;
}
public class Tweet
{
[JsonProperty("atristName")]
public string Artist { get; set; }
[JsonProperty("trackName")]
public string TrackName { get; set; }
}
}
My HomeController.cs class:
public class HomeController : Controller
{
public ActionResult Index()
{
Tweets model = null;
var client = new HttpClient();
var task = client.GetAsync("http://itunes.apple.com/search?term=metallica")
.ContinueWith((taskwithresponse) =>
{
try
{
**var response = taskwithresponse.Result;** Fails here
var readtask = response.Content.ReadAsAsync<Tweets>();
readtask.Wait();
model = readtask.Result;
}
catch (AggregateException e)
{
e.ToString();
}
});
task.Wait();
return View(model.results);
}
As I have built this following a tutorial and am pretty new to web api, I have no idea why it's throwing an error.
Ideas?
I have found very few good tutorials showing how to consume simple webservices with webAPI2 as it's such a new concept. Nor can I find why I'd be getting this error.
Edit:
Forgot to mention that I am running this project on localhost, not sure if that is relevant.
Edit 2:
Exact exception:
{"An error occurred while sending the request."}
InnerException:
{"Unable to connect to the remote server"}
InnerException:
{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 23.3.162.217:80"}
I've tried several different urls and they all run in the browser and none run in my program so I must be missing something?
Upvotes: 1
Views: 3266
Reputation: 34830
If you look at the headers of the response from iTunes you can see that the content type is not application/json
it is text/javascript
.
Having just run your code myself I don't believe the exception throws when you access the response. It will throw when you try and read the response body because the built in media type formatters do not support text/javascript
.
You can add the unsupported media type to the JsonMediaTypeFormatter
or (and perhaps an easier option) you can just read the response body as a string and then perform the JSON deserialization yourself.
Also, async/await is your friend. You'll find it makes working with asynchronous APIs much easier:
public async Task<ActionResult> Index()
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("http://itunes.apple.com/search?term=metallica");
var json = await response.Content.ReadAsStringAsync();
var tweets = JsonConvert.DeserializeObject<Tweets>(json);
return View(tweets);
}
}
Upvotes: 1