Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Virendra 4 posts 24 karma points
    Jul 25, 2013 @ 08:50
    Virendra
    0

    Is there any inbuilt umbraco API which support access to outside data?

    Hi All,        I would like tp know if there any inbuilt umbraco API which allows to access content of a page of a website built on non-umbraco platform. Or in other words is there any umbraco API by using which we can pull content of page of a website.Thanks in advanceViren

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 25, 2013 @ 09:59
    Ismail Mayat
    0

    Virendra,

    No however lets say you have a static html page at some site eg. http://mysite/some.html you could create a razor macro that uses .net httpclient to pull down that page and write out its content in your site. 

    using System.Net; 
    using System.IO;

    WebRequest req = WebRequest.Create("http://mysite/some.html"); 

    WebResponse res = req.GetResponse(); 
    StreamReader sr = new StreamReader(res.GetResponseStream()); 
    string html = sr.ReadToEnd();

    If you wanted to then further process that html and say only get content with a certain div then you could load it into a library like htmlagility pack and xpath out what you need 

    HtmlDocument doc = new HtmlDocument();
     doc.Load(html);
     foreach(HtmlNode div in doc.DocumentElement.SelectNodes("//div"])
     {
       //do some stuff with div.
     }
    

    Regards

    Ismail

  • Funka! 398 posts 661 karma points
    Jul 25, 2013 @ 21:32
    Funka!
    0

    An even simpler, one-line approach I use for fetching external HTTP content:

    string html = new System.Net.WebClient().DownloadString(whateverUrl);

    Be sure to cache this result depending on how frequently the source data is updated. Keep in mind this is a synchronous action, so however long it takes to fetch that remote page will be added on top of the time required for your umbraco page to render.

    If you were fetching resources from multiple pages at once, then it might be worthwhile to look into how to run multiple web requests asynchronously, so that this total time to fetch remote pages is only as long as the longest/slowest one.

    Best of luck to you!

Please Sign in or register to post replies

Write your reply to:

Draft