将HTML路径保存到文件

时间:2012-02-13 06:00:53

标签: c# filestream

编辑我的代码以使用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);

3 个答案:

答案 0 :(得分:0)

WebClient.DownloadFile可能会更容易。

答案 1 :(得分:0)

使用WebClient类代替FileStream,它提供了令人愉快的简单DownloadFile()方法:

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

答案 2 :(得分:0)

试试这个方法。这将为您提供整个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;
    }

希望这会对你有所帮助。