我正在编写一个程序,需要从网站下载.exe
文件,然后将其保存到硬盘。 .exe
存储在我的网站上,它的网址如下(这不是我为这个问题编写的真正的uri):
http://www.mysite.com/calc.exe
经过多次网络搜索,并在这里找到示例,这是我到目前为止提出的代码:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(http://www.mysite.com/calc.exe);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
string s = streamReader.ReadToEnd();
如您所见,我正在使用StreamReader
类来读取数据。在调用 ReadToEnd
之后,流阅读器是否包含我的.exe的(二进制)内容?我可以将StreamReader
的内容写入文件(名为calc.exe),我会成功下载.exe吗?
我想知道为什么StreamReader
ReadToEnd
会返回一个字符串。在我的情况下,这个字符串是否是calc.exe的二进制内容?
答案 0 :(得分:9)
WebClient是下载文件的最佳方法。但您可以使用以下方法从Web服务器异步下载文件。
private static void DownloadCurrent()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("[url to download]");
webRequest.Method = "GET";
webRequest.Timeout = 3000;
webRequest.BeginGetResponse(new AsyncCallback(PlayResponeAsync), webRequest);
}
private static void PlayResponeAsync(IAsyncResult asyncResult)
{
long total = 0;
long received = 0;
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
try
{
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult))
{
byte[] buffer = new byte[1024];
FileStream fileStream = File.OpenWrite("[file name to write]");
using (Stream input = webResponse.GetResponseStream())
{
total = input.Length;
long size = input.Read(buffer, 0, buffer.Length);
while (size > 0)
{
fileStream.Write(buffer, 0, size);
received += size;
size = input.Read(buffer, 0, buffer.Length);
}
}
fileStream.Flush();
fileStream.Close();
}
}
catch (Exception ex)
{
}
}
这里有一个类似的主题 - How to Download the File using HttpWebRequest and HttpWebResponse class(Cookies,Credentials,etc.)
答案 1 :(得分:7)
StreamReader是一个文本阅读器实现,即它应该用于读取文本数据而不是二进制数据。在您的情况下,您应该直接使用基础响应流。
对于下载文件,最简单的方法是使用WebClient.DownloadFile方法。
答案 2 :(得分:2)
这应该直接将文件保存在硬盘上。
using System.Net;
using (WebClient webClient = new WebClient ())
{
webClient.DownloadFile("http://www.mysite.com/calc.exe", "calc.exe");
}
答案 3 :(得分:1)
您应该真正调用StreamReader
对象的Read()
方法,而不是使用Stream
。这将要求您填充读取数据的byte []缓冲区,然后您可以使用StreamWriter
或FileStream
将其写入磁盘。
答案 4 :(得分:1)
我可能有点迟了但是如果没有在调试模式下运行,我的文件总是0kb也有同样的问题。这可能是一个相对简单的答案但是在属性下禁用“DEBUG-Constants”解决了它我
答案 5 :(得分:0)
我创建了一个包含事件的类,以便您可以跟踪下载进度:
using System;
using System.IO;
using System.Net;
using System.Net.Mime;
//event examples: https://www.tutorialsteacher.com/csharp/csharp-event
public class HttpWebRequestDownload
{
private long _totalBytesLength = 0;
private long _transferredBytes = 0;
private int _transferredPercents => (int)((100 * _transferredBytes) / _totalBytesLength);
private string _defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public string downloadedFilePath = String.Empty;
public HttpWebRequestDownload(){
//Windows 7 fix
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
}
public void DownloadFile(string url, string destinationDirectory = default)
{
string filename = "";
if (destinationDirectory == default)
destinationDirectory = _defaultDirectory;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Cache-Control", "no-store");
request.Headers.Add("Cache-Control", "max-age=1");
request.Headers.Add("Cache-Control", "s-maxage=1");
request.Headers.Add("Pragma", "no-cache");
request.Headers.Add("Expires", "-1");
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result)
{
_totalBytesLength = response.ContentLength;
string path = response.Headers["Content-Disposition"];
if (string.IsNullOrWhiteSpace(path))
{
var uri = new Uri(url);
filename = Path.GetFileName(uri.LocalPath);
}
else
{
ContentDisposition contentDisposition = new ContentDisposition(path);
filename = contentDisposition.FileName;
}
using (Stream responseStream = response.GetResponseStream())
using (FileStream fileStream = File.Create(System.IO.Path.Combine(destinationDirectory, filename)))
{
byte[] buffer = new byte[1024*1024]; // 1MB buffer
ProgressEventArgs eventArgs = new ProgressEventArgs(_totalBytesLength);
int size = responseStream.Read(buffer, 0, buffer.Length);
while (size > 0)
{
fileStream.Write(buffer, 0, size);
_transferredBytes += size;
size = responseStream.Read(buffer, 0, buffer.Length);
eventArgs.UpdateData(_transferredBytes, _transferredPercents);
OnDownloadProgressChanged(eventArgs);
}
}
}
downloadedFilePath = Path.Combine(destinationDirectory, filename);
OnDownloadFileCompleted(EventArgs.Empty);
}
catch (Exception e)
{
OnError($"{e.Message} => {e?.InnerException?.Message}");
}
}
//events
public event EventHandler<ProgressEventArgs> DownloadProgressChanged;
public event EventHandler DownloadFileCompleted;
public event EventHandler<string> Error;
public class ProgressEventArgs : EventArgs
{
public long TransferredBytes { get; set; }
public int TransferredPercents { get; set; }
public long TotalBytesLength { get; set; }
public ProgressEventArgs(long transferredBytes, int transferredPercents, long totalBytesLength)
{
TransferredBytes = transferredBytes;
TransferredPercents = transferredPercents;
TotalBytesLength = totalBytesLength;
}
public ProgressEventArgs(long totalBytesLength)
{
this.TotalBytesLength = totalBytesLength;
}
public void UpdateData(long transferredBytes, int transferredPercents)
{
TransferredBytes = transferredBytes;
TransferredPercents = transferredPercents;
}
}
protected virtual void OnDownloadProgressChanged(ProgressEventArgs e)
{
DownloadProgressChanged?.Invoke(this, e);
}
protected virtual void OnDownloadFileCompleted(EventArgs e)
{
DownloadFileCompleted?.Invoke(this, e);
}
protected virtual void OnError(string errorMessage)
{
Error?.Invoke(this, errorMessage);
}
}
这是测试示例:
static void Main(string[] args)
{
HttpWebRequestDownload hDownload = new HttpWebRequestDownload();
string downloadUrl = "http://speedtest.tele2.net/10MB.zip";
hDownload.DownloadProgressChanged += HDownloadOnDownloadProgressChanged;
hDownload.DownloadFileCompleted += delegate(object o, EventArgs args)
{
Debug.WriteLine("Download finished and saved to: "+hDownload.downloadedFilePath);
};
hDownload.Error += delegate(object o, string errMessage) { Debug.WriteLine("Error has occured !! => "+errMessage); };
hDownload.DownloadFile(downloadUrl);
}
private void HDownloadOnDownloadProgressChanged(object sender, HttpWebRequestDownload.ProgressEventArgs e)
{
Debug.WriteLine("progress: "+e.TransferredBytes+" => "+e.TransferredPercents);
}