如何使用httpwebrequest将图像从网站拉到本地文件

我试图使用本地的C#应用​​程序将一些网站上的图像从我的本地机器上的文件。 我正在使用下面列出的代码。 我已经尝试了ASCII编码和UTF8编码,但最终的文件是不正确的。 有没有人看到我在做什么错了? 该url是主动和正确的,并显示图像就好了,当我把地址在我的浏览器。

private void button1_Click(object sender, EventArgs e) { HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create("http://www.productimageswebsite.comhttp://img.dovov.comstock_jpgs/34891.jpg"); // returned values are returned as a stream, then read into a string String lsResponse = string.Empty; HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse(); using (StreamReader lxResponseStream = new StreamReader(lxResponse.GetResponseStream())) { lsResponse = lxResponseStream.ReadToEnd(); lxResponseStream.Close(); } byte[] lnByte = System.Text.UTF8Encoding.UTF8.GetBytes(lsResponse); System.IO.FileStream lxFS = new FileStream("34891.jpg", FileMode.Create); lxFS.Write(lnByte, 0, lnByte.Length); lxFS.Close(); MessageBox.Show("done"); } 

好的形象:D

尝试使用以下代码:

你需要使用一个BinaryReader,因为一个图像文件是二进制数据,因此不用UTF或ASCII编码

编辑:using'ified

 HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create( "http://www.productimageswebsite.comhttp://img.dovov.comstock_jpgs/34891.jpg"); // returned values are returned as a stream, then read into a string String lsResponse = string.Empty; using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse()){ using (BinaryReader reader = new BinaryReader(lxResponse.GetResponseStream())) { Byte[] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10); using (FileStream lxFS = new FileStream("34891.jpg", FileMode.Create)) { lxFS.Write(lnByte, 0, lnByte.Length); } } } MessageBox.Show("done"); 

好的,这是最后的答案。 它使用一个内存stream作为缓冲数据stream的方法。

  private void button1_Click(object sender, EventArgs e) { byte[] lnBuffer; byte[] lnFile; HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create("http://www.productimageswebsite.comhttp://img.dovov.comstock_jpgs/34891.jpg"); using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse()) { using (BinaryReader lxBR = new BinaryReader(lxResponse.GetResponseStream())) { using (MemoryStream lxMS = new MemoryStream()) { lnBuffer = lxBR.ReadBytes(1024); while (lnBuffer.Length > 0) { lxMS.Write(lnBuffer, 0, lnBuffer.Length); lnBuffer = lxBR.ReadBytes(1024); } lnFile = new byte[(int)lxMS.Length]; lxMS.Position = 0; lxMS.Read(lnFile, 0, lnFile.Length); } } } using (System.IO.FileStream lxFS = new FileStream("34891.jpg", FileMode.Create)) { lxFS.Write(lnFile, 0, lnFile.Length); } MessageBox.Show("done"); } 

答案的一个变种,使用asynchronous等待asynchronous文件I / O。 请参阅asynchronous文件I / O为什么这很重要。

使用BinaryReader / Writer下载png并写入磁盘

 string outFile = System.IO.Path.Combine(outDir, fileName); // Download file var request = (HttpWebRequest) WebRequest.Create(imageUrl); using (var response = await request.GetResponseAsync()){ using (var reader = new BinaryReader(response.GetResponseStream())) { // Read file Byte[] bytes = async reader.ReadAllBytes(); // Write to local folder using (var fs = new FileStream(outFile, FileMode.Create)) { await fs.WriteAsync(bytes, 0, bytes.Length); } } } 

读取所有的字节扩展方法

 public static class Extensions { public static async Task<byte[]> ReadAllBytes(this BinaryReader reader) { const int bufferSize = 4096; using (var ms = new MemoryStream()) { byte[] buffer = new byte[bufferSize]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { await ms.WriteAsync(buffer, 0, count); } return ms.ToArray(); } } } 

您可以使用以下方法从网站下载图像并使用Image类保存:

 WebRequest req = WebRequest.Create(imageUrl); WebResponse resp = req.GetResponse(); Image img = Image.FromStream(resp.GetResponseStream()); img.Save(filePath + fileName + ".jpg");