保存文件的HTML路径

本文关键字:路径 HTML 保存文件 | 更新日期: 2023-09-27 18:24:21

编辑了我的代码以使用WebClient。。。仍然不起作用

string hhtmlurl = /Thumbnail.aspx?productID=23&Firstname=jimmy&lastnight=smith;
string strFileName = string.Format("{0}_{1}", hfUserID.Value, Request.QueryString["pid"].ToString() + documentID.ToString());
WebClient client = new WebClient();
client.DownloadFile("http://www.url.ca/" + hhtmlurl.Value + "card=1", strFileName);

保存文件的HTML路径

试试这个方法。这将为您提供整个html内容的字符串返回。把这个字符串写在你想要的任何文件中

public string GetHtmlPageContent(string url)
    {
        HttpWebResponse siteResponse = null;
        HttpWebRequest siteRequest = null;
        string content= string.Empty;
        try
        {
            Uri uri = new Uri(url);
            siteRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            siteResponse = (HttpWebResponse)siteRequest.GetResponse();
            content = GetResponseText(siteResponse);
        }
        catch (WebException we)
        {
            // Log error
        }
        catch (Exception e2)
        {
            // Log error
        }
        return content;
    }
        public string GetResponseText(HttpWebResponse response)
    {
        string responseText = string.Empty;
        if (response == null)
            return string.Empty;
        try
        {
            StreamReader responseReader = new StreamReader(response.GetResponseStream());
            responseText = responseReader.ReadToEnd();
            responseReader.Close();
        }
        catch (Exception ex)
        {
            // Log error
        }
        return responseText;
    }

希望这对你有帮助。

WebClient.DownloadFile可能会更容易。

使用WebClient类而不是FileStream,它提供了非常简单的DownloadFile()方法:

WebClient client = new WebClient();
client.Downloadfile("http://www.url.ca/" + hhtmlurl + "card=1", strFileName);