Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,

I am getting RSS feeds from a URL using HTTPClient, after all codes are written and I test on localhost, RSS feeds gets fetched but after I publish to the server, I get this error.

C#
One or more errors occurred. at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task`1.get_Result() at dayrunnerapp.Controllers.HomeController.callRss(String url)


Here's what I have tried.

What I have tried:

this is the controller view
C#
public ActionResult News()
        {
            try
            {
                var url = "http://www.inboundlogistics.com/cms/rss-feed";
                
                string html = string.Empty;

                html = callRss(url);
                
                XDocument xml = XDocument.Parse(html);
                var RSSFeedData = (from x in xml.Descendants("item")
                                   select new FeedObjects
                                   {
                                       Title = ((string)x.Element("title")),
                                       Link = ((string)x.Element("link")),
                                       test = ((string)x.Element("description").Value),
                                       Description = ((string)x.Element("description").Value.Replace("/cms//", "http://").Replace(")", "")),
                                       Date = ((string)x.Element("pubDate"))
                                   });
                ViewBag.RSSFeed = RSSFeedData;
                ViewBag.URL = url;
            }
            catch (Exception e)
            {
                ViewBag.Error = e.Message+"\n\r"+e.StackTrace;
                Console.WriteLine(e.Message);
            }
            return View();
        }


this is the callRss function
C#
[HttpGet, AllowCrossSiteJson]
       private string callRss(string url)
       {
           string html = String.Empty;

           cli = new HttpClient();
           cli.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
           cli.Timeout = TimeSpan.FromMilliseconds(100000);
           try
           {
               HttpResponseMessage result = cli.GetAsync(url).Result;
               html = Encoding.UTF8.GetString(result.Content.ReadAsByteArrayAsync().Result);
           }
           catch (Exception ex)
           {
               html = ex.Message +"\n\r"+ ex.StackTrace;
           }

           return html;
       }


This works perfectly fine on localhost, without changing anything, I publish and deploy but I get the above mentioned error when I run the URL on the server. Please what haven't I put into consideration yet?
Posted
Comments
Richard Deeming 23-Oct-18 8:11am    
You're going to need to examine the exception to find out what the "one or more errors" are.

I'd be inclined to start by making your code properly async, and to stop swallowing the exceptions:
private async Task<string> callRss(string url)
{
    var cli = new HttpClient();
    cli.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
    cli.Timeout = TimeSpan.FromMilliseconds(100000);
    HttpResponseMessage result = await cli.GetAsync(url);
    result.EnsureSuccessStatusCode();
    return await result.Content.ReadAsStringAsync();
}

public async Task<ActionResult> News()
{
    const string url = "http://www.inboundlogistics.com/cms/rss-feed";
    string html = await callRss(url);
    ...
}
EasyHero 23-Oct-18 23:44pm    
Here's the error after updating my code as you suggested "An error occurred while sending the request. Unable to connect to the remote server 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.38.53.172:80 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)"
Richard Deeming 24-Oct-18 7:31am    
Sounds like a DNS error on your server. That IP address seems to relate to a23-38-53-172.deploy.static.akamaitechnologies.com, not the domain you're trying to load.
Jason Gleim 23-Oct-18 8:12am    
Silly question... have you opened a browser on the server and tried opening the URL in it to confirm you can actually reach the site from the server? The exception is happening inside a task which doesn't do a very good job of surfacing what the real problem is. You might try returning the inner exception and see what you get.

EDIT: I totally missed the missing await! What he said too ^^^^ :-)
EasyHero 23-Oct-18 8:17am    
Yes, i have opened the url on the browser and it returns an xml just as expected

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900