Сохранение пути HTML в файл - PullRequest
0 голосов
/ 13 февраля 2012

Отредактировал мой код для использования 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 голосов
/ 13 февраля 2012

Вместо FileStream используйте класс WebClient, который предлагает восхитительно простой метод DownloadFile():

WebClient client = new WebClient();
client.Downloadfile("http://www.url.ca/" + hhtmlurl + "card=1", strFileName);
0 голосов
/ 13 февраля 2012

Попробуйте этот метод. Это даст вам возврат строки для всего содержимого 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;
    }

Надеюсь, это поможет вам.

0 голосов
/ 13 февраля 2012

WebClient.DownloadFile, вероятно, будет проще.

...