How to download html using WebClient residing on the own server? - asp.net-2.0

How do I make WebClient connect and download htm from its page. I know this is very confusing so let me retierate:
I have written code using WebClient to download htm string of http://www.someserver.com/Invoice.aspx. The Webclient code resides in the Invoice.aspx page and hence is giving me an error like this "A connection attempt failed because the connected party did not properly respond after a period of time". I am not sure how to get the way around it so that I can download the htm page from the page itself.
I will be indebted forever

Can you not use a querystring parameter in your WebClient method call? Then you could check for the querystring parameter and if it does not exist then just render the page and ignore the WebClient call.

Related

Server.Transfer from ASP.NET to ASP

No duplicate of “Server.Transfer from ASP to ASP.Net” ;-)
On an IIS web server (running Classic ASP), I have a local URL that a user is remotely redirected to. Presumably, this call is made with data in the query string or transmitted through POST data. When this request is made, I need to remove this data (especially the query string) server-side, so none will be visible to the client.
For example, the user is led to http://example.com/dir/?data=payload. This is what requested, and this is what the user’s browser will display. Now I need the request resource to strip QueryString and Form data, so that the user ends up in e.g. http://example.com/dir/.
On MSDN, they have HttpServerUtility.Transfer, which adds a boolean to the classic Server.Transfer method allowing to preserve or clear data. However, when I try this in an aspx file transfering to an asp file, I get a 0x80004005 HTTP exception (“No http handler was found for request type 'GET'”).
Is it possible at all to “redirect” from an ASP.NET file to a Classic one?
Is there another, better way to remove request data server-side?
My options would be:
Use a redirect on the page without querystrings: Response.Redirect() This will clear post data as well.
Do a HTTP Request to scrape the HTML of the other page, and view it in your current page.
I would probably do option #1

Iframe and http error when loading invalid or protected URL

IM writing some html which provides a preview function for a list of URLs. I want to use an iframe for this functionality,
The issue arises when some of the URLs are broken (returning a 500 error) or when a page contains some authenication process which the requesting user cannot satisfy. In these situations the iframe is trying to display the URL but the content returned in the frame is useless (500 error or authenication error ) to the user.
DOes iframe have any built in error handling for these senarios or is there some other way i can display a generic error page if something happens when loading the iframe?
Thanks
AFAIK, there is no way to directly access the header of a response to a request initiated by an iframe (or indeed, any request) in client side script.
This is slightly convoluted, but I think it would work:
The iframe is initially loaded with a URL that refers to a script on your server, and your pass the actual URL as a GET parameter.
The server side script takes that URL, and sends a HEAD request to it (following 3xx redirects).
If the response code for the HEAD request is >= 200 and < 300, send some script back to the client which changes the iframe's src to the actual URL (you might be able to do something as simple as window.location.href = but I'm not sure without testing).
If the response code is >= 400, send a page that says "This page is not loading at the moment".
If you know PHP/have it available on your server, I can try and provide a code example.

XML Load TimeOut

I am using below code to load a xml:
XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://mydomain.com/video/list");
in normal situation it works fine, but some times i face a issue of response time out.
sometimes the url from which i wants to load my xml not response me and till that time my application also went timeout.
please tell me what should i do in such situation, so that either i can run my other code if it not responding me within 5 second or any other such solution in which i can do my code in case that url is not responding me xml file.
Thanks
You could try using a HttpWebRequest where you have the possibility to set the TimeOut for the request. In the case the remote resource doesn't response before this timeout value is reached an exception will be thrown which you could intercept and inform the user.

redirecting postdata

I've recently upgraded a page on our server from classic asp to asp.net
The page recieves postdata and saves it to a file. The page is used by many of our clients and the url (to the asp page) is hard coded into their software. This means that i cannot simply swap the old page out for the new one.
I'm trying to find a way to redirect clients from the old url to the new one.
I know you can do a simple redirect using IIS, but this does not cause the postdata to be redirected.
I've tried setting the file to a 307 temporary redirect, this works when the data is in the formdata but other post requests such as ones using the msxml library do not work.
Basically i need to find a way in IIS to forward a post request from one page to another without losing any of the body.
If the two pages are within the same application, you can use Server.Transfer. This just shifts the processing from the old page to the new one, and maintains all of the request data.
The best way i found was to use the temporary redirect code in IIS, although this didn't work for postdata originating from the msxml library. In the end i had to write a COM library in .NET to do the hard work using the system.encoding libraries and then reference the COM library in asp
You can always program (in asp.old) a loop that goes through all the form-data and insert a record with all the values in the database. You then redirect the user to your aspx-page with the id of the row in the database as a querystring parameter. Be careful if the form-data is sensitive, to apply some sort of security to make sure users wont "steal" others data by changing the querystring.
Redirect Reference (IIS 6.0)
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/41c238b2-1188-488f-bf2d-464383b1bb08.mspx?mfr=true

Masking an external URL

I need to be able to open up an external URL in my website with out revealing it to my users (both in the browser and in the source). I do not want them to be able to copy the URL and edit the query string to their liking. Is there a way to open the URL in an iframe, or something of the like, and hide/mask its source?
This is an asp.net 2.0 website.
Could you do the following:
Accept parameters from the user.
Have a webpage or backend process which uses this to download the PDF to a temporary store.
Then stream this to the client, so they don't know about the URL where the PDF is generated? (or just stream directly, without downloading temporarily.)
This way users would never know about the other site, and it should be much more secure.
This could also use some validation/authentication so users are unable to alter the parameters passed to retrieve other users' PDFs.
No. If you are having the clients machine do something (i.e, point their browser to a web page), you can not keep that information from them.
You can render that page server side in a flash widget or some other container but you can't do it on the clients machine.
Best bet: You can make a server-side XMLHTTP request, grab the response and feed in back into your page using AJAX.
You could possibly do it server side by:
Opening a network connection to the
site you want
Obtaining the HTML from a HTML/Get request on the
URL
Inserting it into your page on the server side
That would probably slow down your page load considerably though, and could come coupled with legal issues.
I had a similar problem myself a while ago and did something along these lines (C# .NET 2.0);
public void StreamURLContents(string URL)
{
WebRequest req = WebRequest.Create(URL);
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream dataStream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(dataStream))
{
string currentLine = reader.ReadLine();
while (currentLine != null)
{
Response.Write(currentLine);
currentLine = reader.ReadLine();
}
}
}
You would have to tailor the writing of the HTML to suit your particular application obviously, and you'd break all of the relative links in the target site (image URLs, CSS links etc.), but if you're only after simple text HTML and want your web app to grab it server-side, then this is a good way to go.
You can make a one-time URL by doing the following:
Store a GUID in a database
Allow the client to request the GUID via hyperlink or other means. The GUID is used in the URL as the fake page; example: http://www.company.com/foo/abc-123-efg-456.aspx
Use URL Rewriting to capture and inspect all requests to the directory "foo" and redirect to your handler.
Confirm the GUID is valid, then mark it as "expired" in the database
Send the appropriate data (a PDF?) to the client as the response to the request.
Any subsequent requests to the same URL fail.

Resources