墨香年少 32 发布于 2020年12月25日 public static bool SavePhotoFromUrl(string FileName, string Url) { bool Value = false; WebResponse response = null; Stream stream = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); response = request.GetResponse(); stream = response.GetResponseStream(); if (!response.ContentType.ToLower().StartsWith("text/")) { Value = SaveBinaryFile(response, FileName); } } catch (Exception err) { string errMsg = err.ToString(); } return Value; } private static bool SaveBinaryFile(WebResponse response, string FileName) { bool Value = true; byte[] buffer = new byte[1024]; try { if (File.Exists(FileName)) File.Delete(FileName); Stream outStream = System.IO.File.Create(FileName); Stream inStream = response.GetResponseStream(); int l; do { l = inStream.Read(buffer, 0, buffer.Length); if (l > 0) outStream.Write(buffer, 0, l); } while (l > 0); outStream.Close(); inStream.Close(); } catch { Value = false; } return Value; } 目之所及,皆是回忆,心之所想,皆是过往 分享这篇帖子 链接帖子 分享到其他站点