Server side include external HTML? - c#

In my asp.net-mvc application I need to include a page that shows a legacy page.
The body of this page is created by calling an existing Perl script.
This Perl script is externally hosted.
Is there a way to do something like this:
<!-- #Include virtual="http://www.example.com/theScript.plx"-->

Not as a direct include, because ASP.NET server-side-includes require the page to be compiled at the server.
You could use jQuery to download the HTML from that URL when the page loads, though I appreciate that's not perfect.
Alternatively (and I have no idea whether this will work) you could perform a WebRequest to the perl webpage from your ASP.NET MVC controller, and put the resulting HTML in the view as text. That way you could make use of things like output caching to limit the hits to the perl page if it doesn't change often.

If you wanted to do it all in one go, you could do an HTTP Request from the server and write the contents to the page?
Something like this:
Response.Write(GetHtmlPage("http://www.example.com/theScript.plx"));
Calling this method:
public String GetHtmlPage(string strURL)
{
// the html retrieved from the page
String strResult;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);
objResponse = objRequest.GetResponse();
// the using keyword will automatically dispose the object
// once complete
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return strResult;
}
(Most code ripped blatantly from here and therefore not checked)

You could implement this in a low-key fashion by simply using a frame and setting the frame source to the url that needs to be included. This is quite simple and can be down without any server or client side scripting, so that'd be my preferred approach, if possible.
If you want the html to appear to come from your server, however, you'll need to manually include it - typically by using WebRequest as Neil says. You may wish to cache the remote page for performance, though, since it's a perl script, I'll assume the page is dynamic, so this might not be a great idea.

Related

C# loading html of a webpage currently on

I am trying to make a small app that can log in automatically on a website, get certain texts on the website and return to user.
To show what I have, I did below to make it log in,
System.Windows.Forms.HtmlDocument doc = logger.Document as System.Windows.Forms.HtmlDocument;
try
{
doc.GetElementById("loginUsername").SetAttribute("value", "myusername");
doc.GetElementById("loginPassword").SetAttribute("value", "mypassword");
doc.GetElementById("loginSubmit").InvokeMember("click");
And below to load html of the page
WebClient myClient = new WebClient();
Stream response = myClient.OpenRead(webbrowser.Url);
StreamReader reader = new StreamReader(response);
string src = reader.ReadToEnd(); // finally reading html and saving in variable
Now, it successfully loaded html but html of the page where it's not logged in. Is there a way to refer to current html somehow? Or another way to achieve my goals. Thank you for reading!
Use the Webclient class so you can use sessions and cookies.
check this Q&A: Using WebClient or WebRequest to login to a website and access data
Why don't you make REST API calls and send the data like username and password from your code itself?
Is there any Web API for the URL ? If yes , you can simply call the service and pass on the required parameters. The API shall return in JSON/XML which you can parse and extract information

redirecting to page from inside iframe

I have just now started coding in .NET framework, so my apologies if this issue happens to be of trivial nature.
What I got now
A main.aspx page with simple layout using three iframes
the middle iframe content needs to be dynamic (first a login.aspx page and after logging entryform.aspx)
Issue #1 :
After logging in login.aspx inside the iframe, redirecting to main.aspx
The solution I found:
ClientScript.RegisterStartupScript(this.GetType(),"scriptid",
"window.parent.location.href='main.aspx'", true);
(http://forums.asp.net/t/1273497.aspx)
Issue #2
After redirecting/logging how do I change the middle iframe content from login.aspx to entryform.aspx?
The silly solution I thought of:
Add '#form' to the url and listen to hashchange event in main.aspx. But then, anyone can get to the form using the url itself.
So, basically how do I find a secure way to tell the main.aspx page that it needs to change it's middle iframe content after the redirecting/logging
Or by any chance there is a request.setAttribute and getAttribute in .NET like in java that I have missed and made things difficult for me?
Passing variables or values across pages and domains wont be issue, you can use post method and cross page posting for that
After finding that the use of iframe isn't exactly the best idea in my case, I took Tieson T's advice and looked into HttpClient to fetch content from other web pages. In my case it will be both from same domain and other domains.
Since I have 4.0v .NET instead of HttpClient I used HttpWebRequest
code
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (http://localhost:1706/WebSite3/test.html);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader reader = new StreamReader(response.GetResponseStream(),encode );
string html= reader.ReadToEnd();
myDiv.innerHtml(html);
References
HttpClient does not exist in .net 4.0: what can I do?
http://msdn.microsoft.com/en-us/library/456dfw4f.aspx
http://forums.asp.net/t/1382935.aspx/1
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx
http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx
Wouldn't it be easier to just pass a parameter to Main.aspx? E.g.
ClientScript.RegisterStartupScript(this.GetType(),"scriptid", "window.parent.location.href='main.aspx?LoadEntry=true'", true);
And client-side JavaScript code inside of 'main.aspx' would read that parameter from 'location.search' and if 'LoadEntry=true' is set - would set SRC of middle frame to "entryform.aspx".
Of course inside of "entryform.aspx.cs" you would need to check if correct login really took place (e.g. some Session variable is set) so nobody would be able to simple set URL manually to "main.aspx?LoadEntry=true" to bypass the login.

Load processed post javascript / ajax html into string

I am trying to load processed webpage into string, but seems like it is loading javascript as well; but I want this to be "the final" result that can he saved to static html file and run offline.
This is what I am doing at this moment
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(textBox9.Text);
IWebProxy theProxy = request.Proxy;
if (theProxy != null)
{
theProxy.Credentials = CredentialCache.DefaultCredentials;
}
request.UseDefaultCredentials = true;
request.Proxy = WebRequest.DefaultWebProxy;
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
Any suggestions?
If I understand your post correctly, you don't want to strip the javascript out of the page, but keep it and make it so that it will execute just as though you had visited the page normally in a browser?
This is kind of a notoriously hard problem for proxies to overcome, and others have done it with varying degrees of success. Javascript that is embedded in the page should run just fine, but you will run into problems running any javascript that is loaded into a page from an external file.
One thing you could try is to rewrite the paths to external javascript libraries to reflect a local path, then grab copies of those javascript files over the network as well and store everything in a mimicked directory structure. Your milage may vary based on how fancy the javascript involved is, e.g. some ajax calls probably won't work no matter what you do.

An image calls an ASP utility. How do I do that without image in ASP.NET (C#)

I have a store (C#) that calls our "back office(ASP classic)" via a one-pixel image with some parameters to record a sale. It looks like this:
<asp:Image ID="BOImageLink" runat="server" ImageUrl="https://backoffice.mysite.com/Import.asp?TicketType=Import"></asp:Image>
The store's code-behind appends other parameters to the ImageUrl as needed.
About once per day someone gets through the system without triggering the image tag link. My task is to make the call to that script from our store which is ASP.NET C# code without using an image tag and I just don't know how to do it. All help appreciated, answering accordingly knowing I am new to .NET is doubly so.
That's a really random way of doing that!
Why don't you do a web request in code instead?
http://msdn.microsoft.com/en-us/library/5t9y35bd.aspx
Would be something like this:
WebRequest request = WebRequest.Create ("https://backoffice.mysite.com/Import.asp?TicketType=Import");
using this to get back any response:
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

How to capture HTML of redirect page before it redirects?

I am trying to read the HTML of a page that contains a non-delayed redirect. The following snippet (C#) will give me the destination/redirected page, not the initial one I need to see:
using System.Net;
using System.Text;
public class SomeClass {
public static void Main() {
byte[] data = new WebClient().DownloadData("http://SomeUrl.com");
System.Console.WriteLine(Encoding.ASCII.GetString(data));
}
}
Is there a way to get the HTML of a redirecting page? (I prefer .NET but a snippet in Java or Python would be fine too. Thx!)
Unless the redirect is done on the client side you can't. If the redirect is done server side, then no html is actually generated to the client, but the header is redirected at the new server.
It would take more work, but rather than using WebClient, use HttpWebRequest and set the AllowAutoRedirect property to False. A redirect will then throw an exception, but you can get any response text (and some pages do have response text along with the redirect) from the exception's response object. After you get the response from the exception, you can issue another HttpWebRequest for the redirect URL (specified in the Location response header).
You might be able to do something similar with WebRequest if you create a derived object, MyWebRequest, where you overload the GetWebRequest method and set the AllowAutoRedirect property. I don't know what kind of exception, if any, the DownloadData method will return if you do something like that.
As somebody said previously, this will only work for those pages that do client-side redirects (typically 301 or 302). If there is server-side redirection going on, you'd never know it.
Simplest answer would be to add the current page onto the QueryString component of the redirect when redirecting, for instance:
Response.Redirect(newPage + "?FromPage=" + Request.Url);
Then the new page could see where you cane from by simply looking at Request.QueryString("FromPage").
If you want to get the source of an html page you can use this tool:
http://www.selfseo.com/html_source_view.php

Categories