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")
?