C#如何实现从网站上下载文件
我想用C#实现从网站上下载文件的功能,我只知道该网站的链接,而且链接是有规律的变化的,不知道用C#如何实现呐???期待……多谢了
public Uri fixurl(string url, Uri baseurl) { if (url.IndexOf("javascript:") >= 0) { return null; } Uri u = null; Uri.TryCreate(baseurl, url, out u); return u; }httpwebrequest获取页面内容
/// <summary> /// 获取网页源代码 /// </summary> /// <param name="strLink"></param> /// <returns></returns> public static string GetHtml(string strLink) { Encoding encoding; StreamReader reader = null; string strResults; HttpWebRequest request = null; HttpWebResponse response = null; try { if (strLink.IndexOf(@"http://") >= 0) { request = (HttpWebRequest)WebRequest.Create(strLink); } else { request = (HttpWebRequest)WebRequest.Create(string.Format(@"http://{0}",strLink)); } switch ((DateTime.Now.Second % 2)) { case 0: request.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;.NET CLR 2.0.50727)"; break; default: request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24"; break; } response = (HttpWebResponse)request.GetResponse(); if (response.CharacterSet != null && response.CharacterSet.ToLower() == "utf-8") { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(936); } reader = new StreamReader(response.GetResponseStream(), encoding); strResults = Regex.Replace(reader.ReadToEnd(), @"[\r\n]", " ", RegexOptions.IgnoreCase); if (strLink != response.ResponseUri.ToString()) { strResults = "|RedirectWeb|" + strResults; } return strResults; } catch { return string.Empty; } finally { if (request != null) { request.Abort(); } if (response != null) { response.Close(); } if (reader != null) { reader.Close(); reader.Dispose(); } } }