0

I have to get html response from another website and load to my application. I written below code,

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;

    namespace MVC_TEST.Controllers
    {
        public class DocumentCloudController : Controller
        {
            public string Index()
            {
                var result = GetResponse();
                return result;
            }

            private static string GetResponse()
            {

                var html = string.Empty;
                const string url = @"http://localhost/xxxxx/yyyyy/logon.aspx";

                var request = (HttpWebRequest)WebRequest.Create(url);
                request.AutomaticDecompression = DecompressionMethods.GZip;
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                html = reader.ReadToEnd();
                            }
                        }
                    }
                }


                return html;
            }}


        }
    }

Controls are loaded properly, But image, css and js path is mapped to relative path

    /xxxx/yyyy/dojo.js , 
    /xxxx/style/logon.css, 
    /xxxx/images/logon.png

in html, here i have to change that to actual url like below

 http://localhost/xxxx/yyyy/dojo.js , 
 http://localhost/xxxx/style/logon.js , 
 http://localhost/xxxx/images/logon.png     

One option is find these content in html replace that.

Is there any other option to change url dynamically ? Is IIS URL Rewrite module will suitable for my requirement ?

Please share your thoughts

1 Answer 1

1

Using IIS URL Rewrite Module could work but I would recommend using a HTML parser like HTML Agility Pack or AngleSharp to query and manipulate the DOM.

The example below is a snippet that worked for me when creating a reverse proxy:

foreach (var link in document.DocumentNode.SelectNodes("//link[@href]"))
{
    var orgHrefValue = link.GetAttributeValue("href", string.Empty);
    var updHrefValue = string.Concat("[BASE URL]", GetAbsoluteUrlString(requestedUrl, orgHrefValue).AbsoluteUri);
    link.SetAttributeValue("href", updHrefValue);
}

private static Uri GetAbsoluteUrlString(string baseUrl, string url)
{
    var uri = new Uri(url, UriKind.RelativeOrAbsolute);

    if (!uri.IsAbsoluteUri)
        uri = new Uri(new Uri(baseUrl), uri);

    return uri;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @JesseJhonson . but i have some ajax webservice call in js file, I need to replace that too. I dont know how many service call will come in future ? Is there any other solution ?
I used the same method above to replace absolute URL's in linked CSS files and JS scripts. Have you looked into using a reverse proxy? Something like Titanium-Web-Proxy might simplify the process. Essentially you parse the HTML and replace all URLS to instead point to your reverse proxy (something like mydomain.com/proxy.ashx?uri={original url}). This way all external files (js, css, etc.) will be loaded through your proxy which you can grab the content, parse, modify and serve the same way you are doing with HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.