/// <summary> /// 下载文件 /// </summary> /// <param name="url">url</param> /// <param name="savePath">文件保存路径</param> /// <param name="header">附加头</param> public static void DownloadFile(string url, string savePath, Dictionary <string, string> header) { using (HttpWebResponse response = HttpHelper.GetHttpWebResponse(url, null, header, Encoding.UTF8)) { using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (Stream stream = response.GetResponseStream()) { int length = 0; do { byte[] bytes = new byte[1024]; length = stream.Read(bytes, 0, bytes.Length); if (length > 0) { fs.Write(bytes, 0, length); } else { break; } } while (true); } } } }
public static string HttpGetString(string url, Dictionary <string, string> header, Encoding encoding) { using (HttpWebResponse respose = HttpHelper.GetHttpWebResponse(url, null, header, encoding)) { using (StreamReader reader = new StreamReader(respose.GetResponseStream(), encoding)) { return(reader.ReadToEnd()); } } }
public static HttpWebResponse HttpGetEx(string url, Encoding encoding) { return(HttpHelper.GetHttpWebResponse(url, null, null, encoding)); }