Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save a url as a html page on the server

Tags:

c#

.net

I have a url and on entering this url, i am getting a html page. What i want is to save that html page on the server from that url. Here is the code-

 protected void one_Click(object sender, EventArgs e)
    {
        string s = textbox.Text;
        var code = s.Split(new[] { "mailerhtml" }, StringSplitOptions.None)[1];
        string product = code.Replace(@"/", string.Empty);
}

In the above code i am entering a url and on one_Click of a button(one) i am saving the textbox text in a string 's'. Additionally, variable 'code' is used to get the name by which i will we saving the html page. for eg- if code is'abc', i will be saving it as code.html Now, i have a folder named 'HTMLPages' on my server and i want to save the html pages in this folder. How can i achieve this?

like image 562
manu sharma Avatar asked Nov 30 '25 10:11

manu sharma


1 Answers

You have to use web client for that purpose it will download it and you can write a file where you want.

 WebClient myClient = new WebClient();
        string myPageHTML = null;
        byte[] requestHTML; 
        // Gets the url of the page
        string currentPageUrl = Request.Url.ToString();

        UTF8Encoding utf8 = new UTF8Encoding();

        // by setting currentPageUrl to url it will fetch the source (html) 
        // of the url and put it in the myPageHTML variable. 

       // currentPageUrl = "url"; 

        requestHTML = myClient.DownloadData(currentPageUrl);

        myPageHTML = utf8.GetString(requestHTML); 

        System.IO.File.WriteAllText(@"C:\yoursite.html", myPageHTML);
like image 81
Hamza Haider Avatar answered Dec 02 '25 00:12

Hamza Haider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!