gpt4 book ai didi

c# - 如何在服务器上将 url 保存为 html 页面

转载 作者:行者123 更新时间:2023-11-30 16:39:25 26 4
gpt4 key购买 nike

我有一个 URL,输入这个 URL 后,我得到了一个 HTML 页面。我想要的是将该 html 页面从该 url 保存在服务器上。这是代码-

 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);
}

在上面的代码中,我输入了一个 url 并在单击按钮(一个)时将文本框文本保存在字符串“s”中。此外,变量“代码”用于获取我将用来保存 html 页面的名称。例如,如果代码是“abc”,我会将其保存为 code.html现在,我的服务器上有一个名为“HTMLPages”的文件夹,我想将 html 页面保存在该文件夹中。我怎样才能做到这一点?

最佳答案

您必须为此目的使用 Web 客户端,它会下载它,您可以在需要的地方写入文件。

 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);

关于c# - 如何在服务器上将 url 保存为 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com