2012-02-13 175 views
0

编辑我的代码中使用Web客户端......还是不工作保存HTML文件路径

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

它是如何工作的?它有什么作用? – svick 2012-02-13 17:23:50

回答

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

希望这会对你有所帮助。

+0

在此代码中,我是否保存该文件并将其存入我自己的文件名? – user979331 2012-02-13 15:10:46

+0

该部分在此处不可用,您必须执行该操作。 l我上面的方法,它会返回整个网页内容的字符串。然后使用Streamwriter(http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx)将该字符串写入另一个文件 – 2012-02-13 17:22:51

+0

Worked !!!了不起的家伙! – user979331 2012-02-15 15:14:08

0

相反的FileStream的,使用WebClient类,它提供了简单的兴高采烈方法DownloadFile()

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

嗯...它似乎没有工作....我已经注意到,我的client.Downloadfile是不是像你的绿色..黑色我使用System.net,我错过了什么? – user979331 2012-02-13 06:18:50

+0

请确保你的代码中有'使用System.Net;'指令,或者显式调用'System.Net.WebClient client = new System.Net.WebClient()' – 2012-02-13 06:24:11

+0

还没有任何事情:(我在 – user979331 2012-02-13 06:33:32