title: Execute HTML script from C#
body: <p>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.</p>
  
  <p>Code is:</p>
  
  <pre><code>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 
          }
  </code></pre>
  
  <p>At the response variable I get following response:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
          &lt;head&gt;
              &lt;script type="text/javascript"&gt;
                  function closethisasap() {
                      document.forms["redirectpost"].submit();
                  }
              &lt;/script&gt;
          &lt;/head&gt;
          &lt;body onload="closethisasap();"&gt;
          &lt;form name="redirectpost" method="post" action="&lt;redirection URL&gt;"&gt;
  
          &lt;/form&gt;
          &lt;/body&gt;
          &lt;/html&gt;
  </code></pre>
  
  <p>I searched for a long time to find a way how to run this HTML response. But haven't find any.</p>
  
  <p>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 <code>HttpContext.Response.Redirect("URL")</code> ?</p>