Save a HTML page to File

by Mark Deraeve 30. May 2008 22:06

When I print a report, I create a html page that the user can print. Now, there was a need to backup these reports. So I had to find out how to save the html code to a file. The solution was very simple:

System.Net.WebClient client = null;
try
{
    client = new System.Net.WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    client.Headers = new System.Net.WebHeaderCollection();
    client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)");
    string url = Request.Url.ToString().Replace("administration","reports");
    url = url.Replace("reports.aspx", "reportprint.aspx?reportGuid=" + guid);
    //string html = client.DownloadString(url);
    string path = ConfigurationManager.AppSettings["ArchivePath"] + guid +".html";
    client.DownloadFile(url, path);
 
    
}
catch (Exception r) 
{
    HandleError(r);
}
finally
{
    client.Dispose();
}
 

The url string had to be the full url, no relative url's. So I took the url of the current page and replaced 2 parts of the url to make the correct url. The path where the report are saved is stored in the config file. 

All this worked fine until I deployed it to the server. Then I got this lovely error:

 The remote server returned an error: (401) Unauthorized.System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.WebClient.DownloadFile(Uri address, String fileName) at System.Net.WebClient.DownloadFile(String address, String fileName) … 

The solution is simple, you need to pass credentials to the proxy. If the default credentials are ok, just set the usedefaultcredentials flag to true. The proxy will now detect the default credentials and use these. When your website is set to Use integrated windows authentication, then this the default credentials should pass your proxy and the problem should be solved.

 
System.Net.WebClient client = null;
try
{
    client = new System.Net.WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    client.Headers = new System.Net.WebHeaderCollection();
    client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)");
    client.UseDefaultCredentials = true;
    string url = Request.Url.ToString().Replace("administration","reports");
    url = url.Replace("reports.aspx", "reportprint.aspx?reportGuid=" + guid);
    //string html = client.DownloadString(url);
    string path = ConfigurationManager.AppSettings["ArchivePath"] + guid +".html";
    client.DownloadFile(url, path);
 
 
}
catch (Exception r) 
{
    HandleError(r);
}
finally
{
    client.Dispose();
}


Tags:

|

Comments (82) -

endangered animals
endangered animals
12/23/2010 10:38:57 PM #

when i came here i assumed it was going to be filled with obvious advice, but actually it turned out to be really useful. keep writing!

web hosting
web hosting
12/23/2010 11:13:19 PM #

Great Share!

tv online
tv online
12/24/2010 12:36:29 AM #

watch tv online - htt://www.tv-online-live.com

wild animals
wild animals
12/24/2010 6:38:17 AM #

when i stumbled upon this i figured it was going to be filled with boring info, but actually it turned out to be quite useful. keep writing!

Frank Mueller
Frank Mueller
12/24/2010 7:23:21 AM #

This weblog is superior it has acquired the whole points i sought after to communicate about, it has fulfilled my wisdom, i just appreciated this weblog and that i want to subscribe so can you please inform although your blog will get as much as date and what?s the procedure to subscribe in details.

Jocuri online
Jocuri online
12/24/2010 6:18:52 PM #

Wow!, this was a top quality post. In theory I’d like to write like this too – taking time and real effort to make a good article… but what can I say… I keep putting it off and never seem to achieve anything.

Las Vegas Web Marketing
Las Vegas Web Marketing
12/24/2010 11:56:32 PM #

Amazing freakin blog here. I almost cried while reading it!

Buy Backlinks
Buy Backlinks
12/25/2010 4:11:10 AM #

High quality info here! Keep up the great work. I love the feelings being expressed.

Wanetta Abbington
Wanetta Abbington
12/25/2010 8:37:08 AM #

The difficulty using Plr blog posts and articles is the fact buyers fail to apply it appropriately.  They should either make use of it for getting tips for things to write about or maybe totally re-write it or " spin " it up and later place their own identify onto it.

Financial Services
Financial Services
12/25/2010 10:00:52 AM #

Interesting layout on your blog. I really enjoyed reading it and also I will be back to read more in the future.

Top seo company
Top seo company
12/25/2010 6:47:37 PM #

Howdy, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it's driving me mad so any assistance is very much appreciated.

tattoos fonts
tattoos fonts
12/26/2010 8:09:39 AM #

you got some great ideas there. I did a search on the issue and learnt most peoples will agree with your blog. If you are an experienced traveler, you may know the ins and outs of travel to all parts of the world, and you may have very specific ideas about what you want to see and where you want to go.

filmy online
filmy online
12/26/2010 12:08:54 PM #

What about Dina G? BTW,Page Bookmarked

Call Center Jobs
Call Center Jobs
12/26/2010 1:35:26 PM #

Hey how are you doing? I just wanted to stop by and say that it's been a pleasure reading your blog. I have bookmarked your website so that I can come back & read more in the future as well. plz do keep up the quality writing

order cheap plavix
order cheap plavix
12/26/2010 4:44:08 PM #

Between me and my husband we've owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I've settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

Forest Mcmeen
Forest Mcmeen
12/26/2010 8:14:29 PM #

I have come back to your website serveral times. The additional articles are very fascinating and out of the ordinary. I wanted to signup for your rss feed,  so I can keep up to date of your latest articles.

sms blast
sms blast
12/27/2010 4:41:51 AM #

Excellent blog! I actually love how it is easy on my eyes and also the data are well written. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day!

Clogged Drain Repair Tyler TX
Clogged Drain Repair Tyler TX
12/27/2010 7:20:17 AM #

I agree with your thoughts here and I really love your blog! I've bookmarked it so that I can come back & read more in the future.

tattoo fonts for men
tattoo fonts for men
12/27/2010 10:11:07 AM #

I really loved reading your blog. It was very well written and simple to undertand. Unlike additional blogs I have read. I also found it very interesting. In fact after reading, I had to go show the spouse and she ejoyed it as well!

find address from cell phone number
find address from cell phone number
12/27/2010 9:04:38 PM #

It is by far the most relevant and up to date information I came across on this subject. Absolutely pleased that I came across your web page by chance. I’ll be subscribing to your site so that I can get the newest news. Thanks for all the knowledge here.

how to get rid of acne over night
how to get rid of acne over night
12/27/2010 10:48:28 PM #

I have looked over your site articles, it is especially worth of noteand informative. I have book marked your website and will revisit repeatedlyto study more of your site.

free rewards points
free rewards points
12/28/2010 5:23:59 AM #

cool u hit it 0n the dot will submit to reddit

best e cigarette
best e cigarette
12/28/2010 1:35:26 PM #

170. I quit this time (3rd attempt) on April 1st due to US federal and state tax increases on cigarettes. I just couldn't afford it any more. I also started working out in order to keep from gaining weight. By May 1st, I still gained 10 pounds and was just as irritable as the previous times that I quit. That's when I found out about and purchased my e-cigarettes. I've lost the 10 pounds and and am no longer irritable. I'm also still working out.

Vacanze In Italia
Vacanze In Italia
12/28/2010 8:53:52 PM #

Amazing freakin blog here. I almost cried while reading it!

Video Game Release Dates
Video Game Release Dates
12/29/2010 2:35:12 AM #

Great post! I'm surely sharing this with my friends! Had fun reading it indeed.

Nikki Watkins
Nikki Watkins
12/29/2010 3:04:57 AM #

I have read many blogs and found good and informatics but this blog has simply thrill me with it designs, colour combination and the most important thing real information. I have found only genuine and useful information on this blog.

Video Game Release Dates
Video Game Release Dates
12/29/2010 3:55:40 AM #

Great post! I'm surely sharing this with my friends! Had fun reading it indeed.

Video Game Release Dates
Video Game Release Dates
12/29/2010 3:57:42 AM #

Great post! I'm surely sharing this with my friends! Had fun reading it indeed.

Video Game Release Dates
Video Game Release Dates
12/29/2010 3:58:36 AM #

Great post! I'm surely sharing this with my friends! Had fun reading it indeed.

Video Game Release Dates
Video Game Release Dates
12/29/2010 3:59:36 AM #

Great post! I'm surely sharing this with my friends! Had fun reading it indeed.

Video Game Release Dates
Video Game Release Dates
12/29/2010 4:00:54 AM #

Great post! I'm surely sharing this with my friends! Had fun reading it indeed.

Black Magic Spells
Black Magic Spells
12/29/2010 4:51:22 AM #

FCC to vote on 'net neutrality' The Federal Communications Commission is set to vote Tuesday on set of regulations desi

Seo Services
Seo Services
12/29/2010 7:04:22 AM #

I would like to start my own blog one day. This was a really nice blog that you made here. Keep up the success Tong

web hosting
web hosting
12/29/2010 8:57:27 AM #

just check this: http://www.hostbyweb.com

add a link
add a link
12/29/2010 3:55:32 PM #

There are only few ways to can learn this.

Backlinks
Backlinks
12/29/2010 4:47:14 PM #

I would like to know more about how you can work this to make money online.  It seems like a lot of the things about making money and capitalizing on these types of ideas is so basic that everybody is doing it, and really the only people making riches from it are folks advertising the instructions.  Can you do a blog post about how you use this to actually make money?

stuff for free
stuff for free
12/29/2010 6:26:12 PM #

very nice blog, keep it up, will share this to others.

add a link
add a link
12/29/2010 9:20:19 PM #

What about social bookmarking?

Filmy Online
Filmy Online
12/29/2010 11:00:52 PM #

And how you can do social bookmarking?

Backlinks
Backlinks
12/29/2010 11:53:37 PM #

Related articles regarding this do not provide near as much information, heck you generally have to pay to look at information similar to this HAHA!  Thanks so much for your donations to this blog, you make it worth reading.  I'll be back tomorrow!\

Hypercom T7Plus Manual
Hypercom T7Plus Manual
12/30/2010 12:57:44 AM #

Do you like running Blog Engine?  If I might --perhaps you should consider adding a few images.  I dont mean to disrespect what you've done; its very good, for sure.  However, I think would respond to it more positively if you could add something animated to your postings.  Keep it up, but put a little more into it next time.

videos women giving birth
videos women giving birth
12/30/2010 4:25:38 AM #

You should wite a blog post about a recommendation of websites like Dailycode.Info | Save a HTML page to File. I came across your blog two days ago when I was googling, however I am not really into the blog thing. I don't think it's because I do not enjoy blogs, but more than likely due to the fact that I'm a little ignorant to them.  But blogengine is cool.

cheap air max 2009
cheap air max 2009
12/30/2010 9:48:32 AM #

I can not believe that your opinions on this topic are so close to mine. Thank you for sharing it which make me more confidence on my stand.

Black Magic Spells
Black Magic Spells
12/30/2010 2:01:50 PM #

If a lender gives a mortgage with a payment triple a borrower’s income, who do we blame the foreclosure on? Here&

pozycjonowanie stron www
pozycjonowanie stron www
12/30/2010 5:21:24 PM #

It is truely nice post, but I do not see everything completely clear, especially for someone not involved in that topic. Anyway very interesting to me.

P1664 Ford
P1664 Ford
12/30/2010 6:51:25 PM #

After a jiffy, regular commenters begin to create a community. It starts to feel intrusive to insert yourself.

Ddene Pauley
Ddene Pauley
12/31/2010 2:17:39 AM #

Hi Sir, I was quite surprised by your blog, could you give me your contact email? I would like to discuss something. Thanks.

tattoo fonts script
tattoo fonts script
12/31/2010 4:58:59 AM #

Do you folks have a fb fan web page? I seemed for one on twitter however could not uncover one, I would like to become a fan!

SEO Agency
SEO Agency
12/31/2010 7:11:39 AM #

Really nice post! Your writing is so refreshing in comparison to most other bloggers. Thanks for writing when you do, I'll be sure to read more!

games for wii fit
games for wii fit
12/31/2010 10:42:01 AM #

Any existing pretty good Nintendo wii gaming console video games?  I was thinking if anyone have any kind of solutions? I am just thinking about selling my Nintendo wii and using the bucks to get new Xbox 360 Console games or maybe a Ps3... It occurred to me i always use my Nintendo wii console much more to play my older Nintendo gamecube when i previously own a Nintendo gamecube, funny !. So if anybody can tell me some terrific Nintendo wii games  I'd be interested to check out if they are good enough that I'll hold my own Wii. Thank you.

web hosting
web hosting
12/31/2010 12:55:15 PM #

just check this: http://www.hostbyweb.com

college grants
college grants
12/31/2010 3:47:20 PM #

You are a very bright person!

Black Magic Spells
Black Magic Spells
12/31/2010 6:49:26 PM #

What’s The Best Legitimate Way To Make Money Online Working From Home ?: I’m not interested in online MLM or netwo

tv online
tv online
12/31/2010 7:34:43 PM #

I wanna recomand you a nice website to watch your favorites tv channels online: http://www.tv-online-live.com

best linux distro
best linux distro
12/31/2010 11:15:35 PM #

very interesting info  ! .

Aion accounts
Aion accounts
12/31/2010 11:27:10 PM #

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

free online porn
free online porn
1/1/2011 1:37:24 AM #

Usually I don’t post on blogs, but I need to say that this write-up really forced me to complete so! Thanks, extremely nice article.

Magic Submitter
Magic Submitter
1/1/2011 1:51:24 AM #

Need a Marketing Manager ?If you are serious about growing your profits,there is not a single tool available that is more powerful and effective than Magic Submitter <a href="http://marketing.singledad.de">Ultimate Marketing Tool - Video</a>

Marketing Software
Marketing Software
1/1/2011 2:17:07 AM #

Managing your Marketing Campaign.It automatically submits it to Video Sites, Photo Sites, Blogs, Micro Blogs, Bookmarking Sites, RSS Feeds,Press Release Sites, and dozens more.<a href="singledad.de/businesspromoting">Marketing Tools</a>

Magic Submitter
Magic Submitter
1/1/2011 2:17:39 AM #

Your Marketing Manager.It gives you more free traffic at the push of a button, I was completely blown away by it.This thing is so cool. <a href="http://goo.gl/maps/eaZ8">Marketing Software</a>

download youtube
download youtube
1/1/2011 2:25:59 AM #

Have you tried freecorder before for downloading youtube?  If so - got any feedback? Would love to hear your review.

tera patrick blow job
tera patrick blow job
1/1/2011 3:50:56 PM #

I have studied a few of the articles on your blog, and I genuinely like your style of writing. I added it to my bookmarks blog site directory and will be coming back in the near future.

la gazzetta dello sport
la gazzetta dello sport
1/1/2011 7:46:44 PM #

What about social bookmarking?

casino online
casino online
1/2/2011 2:00:54 AM #

Hi, I just found your blog - thanks for the good work. Just wanted to let you know that it's not displaying correctly on the BlackBerry Browser (I have a Bold). Either way, I am now on the RSS feed on my home PC, so thanks again!

Sohbet
Sohbet
1/2/2011 3:00:01 AM #

If you're still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you'll know which is right for you.

Casandra Nurnberger
Casandra Nurnberger
1/2/2011 3:27:01 AM #

"This web site is really a walk-through for all of the info you wanted about"

online casino
online casino
1/2/2011 3:43:14 AM #

Hi. I read a few of your other posts and wanted to know if you would be interested in exchanging blogroll links?

casino online
casino online
1/2/2011 3:44:34 AM #

Hi, interesting post. I have been pondering this issue,so thanks for posting. I will visit your blog again

online casino
online casino
1/2/2011 3:46:08 AM #

I just wanted to take a minute to say thanks for posting this. This area was on my mind recently, as a result I sincerely loved this article!

casino online
casino online
1/2/2011 3:47:55 AM #

Hi, interesting post. I have been pondering this issue,so thanks for posting. I will visit your blog again

online casino
online casino
1/2/2011 3:50:26 AM #

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Link Building
Link Building
1/2/2011 4:46:53 AM #

This is the best blog I've ever seen in my life! I really appreciate you taking the time out of your busy day to share your this with everyone.

Sohbet
Sohbet
1/2/2011 5:25:12 AM #

Between me and my husband we've owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I've settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

Sohbet
Sohbet
1/2/2011 5:27:05 AM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.

Sohbet
Sohbet
1/2/2011 5:29:24 AM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.

Sohbet
Sohbet
1/2/2011 5:31:59 AM #

Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It's very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

Jack Crummie
Jack Crummie
1/2/2011 5:34:13 AM #

"you’re not the average blog author, man"

Sohbet
Sohbet
1/2/2011 5:35:49 AM #

If you're still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you'll know which is right for you.

Elnora Holmes
Elnora Holmes
1/2/2011 7:28:26 PM #

If you are short on initial funds, investing in domains can generate real and serious returns. Having sold many .com's for very good 5 figures, I know the market inside and out. It's tough out there, but the returns are there if you are careful and know what to look for.

Backlinks
Backlinks
1/2/2011 11:15:03 PM #

I agree with your thoughts here and I really love your blog! I've bookmarked it so that I can come back & read more in the future.

tori black pic
tori black pic
1/3/2011 1:02:13 AM #

I have studied a few of the articles on your blog, and I genuinely like your style of writing. I added it to my bookmarks blog site directory and will be coming back in the near future.

million hits secret
million hits secret United States
8/9/2014 11:44:18 AM #

You completed various fine points there. I did a search on the topic and found a good number of folks will go along with with your blog.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

I started as a VB6 developer, programming all kinds of applications. I also got involved in the development of a e-market in ASP.

When .Net was introduced, I had the privilege to start in a team of professionals and develop a framework. I learned a lot and started to work as a c#.Net consultant from then on.

After 2 years I also got the chance to work for several months with SharePoint. SharePoint kept chasing me all the years after and when the product got more and more mature I started to love it. Then SharePoint was a daily occupation for me. I implemented a large quality document management system at PMRL being the only developer/technical analyst. Working close together with the QA department we succeeded in making and improving a very good Quality document management system. Its used up until this day.

I worked for 6 years as a .Net consultant for 2 of the largest consultancy companies in Belgium. One of my last projects with the consultancy was creating a custom LIMS system in c#.Net. This was a success and the customer asked me to stay and help to expand and maintain this LIMS. I had great years in this company. There was a focus on innovation and a lot of opportunities for improving and developing new IS systems. Apart from several small projects I further more improved the custom LIMS system, created a SOP library based on SharePoint and a web based Skill matrix system to handle the skills of the employees in the company. 

Then I was involved in transferring the LIMS to a new lab in Singapore. This was done in a short time frame. The deployment was again a success and it passed the GLP inspection tests. I learned a lot from the Quality engineers about validation of computerized systems.

At my current job I have a wide range of challenges. Mainly I'm still working with .Net (VB.net and C#.Net). But outside of the Microsoft world I also do some ABAB programming in our SAP system. Like developing Adobe Forms and the program behind it. Or making small changes to customized code.

I'm working for 2 years with objective C now. Creating apps for factories across Europe. People in the factory are working with IPad to take pictures and upload them to orders and more. Already more and more projects are coming my way. The iOS apps are supported by a home made .Net architecture that exposes WCF services. Its working great. My second app is almost ready for deployment in factories. Now I started an App for the management team so they can manage projects on their IPAD and IPHONE. Next generation apps are build with a .Net Web API background combined with EF 6.0.

My linkedIn!

Month List

Tag cloud

Locations of visitors to this page