0

The scenario is that I've to call a service from my code, it returns a block of HTML to me and then I need to execute that code to redirect another page.

Code is:

var request = (HttpWebRequest)WebRequest.Create("Destination URL");
        request.Method = WebRequestMethods.Http.Post;
        request.ContentType = MimeTypes.ApplicationXWwwFormUrlencoded;
        request.ContentLength = postData.Length;

        try
        {
            using (var stream = request.GetRequestStream())
            {
                stream.Write(postData, 0, postData.Length); //postData holds post parameters
            }
            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var response = streamReader.ReadToEnd();
            }
        }
        catch (Exception ex)
        {
            //Error handling 
        }

At the response variable I get following response:

<html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <script type="text/javascript">
                function closethisasap() {
                    document.forms["redirectpost"].submit();
                }
            </script>
        </head>
        <body onload="closethisasap();">
        <form name="redirectpost" method="post" action="<redirection URL>">

        </form>
        </body>
        </html>

I searched for a long time to find a way how to run this HTML response. But haven't find any.

Is there any way either to run the HTML to automatically redirect to the desired destination or parse the HTML and grab form action value and run HttpContext.Response.Redirect("URL") ?

Ratul
  • 302
  • 6
  • 20
  • 2
    You don't "execute" HTML code. And there's nothing in this code that performs a "redirect". Are you just trying to parse out the `action` attribute value from the `form` element? A DOM parser would help with that. Something like HTMLAgilityPack for C# for example. That would take the HTML response as a string and you could query it for structured data within that HTML, such as an attribute from a specific tag. – David Feb 16 '17 at 17:06
  • If I don't have a way to execute that HTML, then there is maybe only 1 way to parse the `action` attribute. Thanks for the suggestion. I'm going to try with that library. – Ratul Feb 16 '17 at 17:42

1 Answers1

0

OK. After getting the suggestion from @David, I am able to parse the HTML and successfully redirect to the desired URL. Following is my solution (with the help of https://stackoverflow.com/a/847051/897504):

HtmlDocument html = new HtmlDocument();
                    html.LoadHtml(streamReader.ReadToEnd());

                    // ParseErrors is an ArrayList containing any errors from the Load statement
                    if (html.ParseErrors != null && html.ParseErrors.Count() > 0)
                    {
                        // Handle any parse errors as required
                    }
                    else
                    {

                        if (html.DocumentNode != null)
                        {
                            HtmlNode formNode = html.DocumentNode.SelectNodes("//form[@action]").FirstOrDefault();

                            if (formNode != null)
                            {
                                HtmlAttribute att = formNode.Attributes["action"];
                                if (att != null)
                                    _httpContext.Response.Redirect(att.Value);
                                else
                                    _httpContext.Response.Redirect(storeLocation);
                            }
                        }
                    }

Thank you very much.

Community
  • 1
  • 1
Ratul
  • 302
  • 6
  • 20