Response从Web服务器下载二进制

Response从Web服务器下载二进制

本文介绍了如何使用HttpWebRequest/Response从Web服务器下载二进制(.exe)文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序需要从网站下载 .exe 文件,然后将其保存到硬盘中. .exe 存储在我的网站上,其网址如下(不是真正的uri,我只是为解决此问题而编写的):

I am writing a program that needs to download an .exe file from a website and then save it to the hard drive. The .exe is stored on my site and it's url is as follows (it's not the real uri just one I made up for the purpose of this question):

http://www.mysite.com/calc.exe

经过多次网络搜索并摸索了示例,这是我到目前为止提出的代码:

After many web searches and fumbling through examples here is the code I have come up with so far:

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吗?

As you can see I am using the StreamReader class to read the data. After calling ReadToEnd does the stream reader contain the (binary) content of my .exe? Can I just write the content of the StreamReader to a file (named calc.exe) and I will have succesfully downloaded the .exe?

我想知道为什么 StreamReader ReadToEnd 返回一个字符串.就我而言,该字符串是否为calc.exe的二进制内容?

I am wondering why StreamReader ReadToEnd returns a string. In my case would this string be the binary content of calc.exe?

推荐答案

WebClient是下载文件的最佳方法.但是您可以使用以下方法从Web服务器异步下载文件.

WebClient is the best method to download file. But you can use the following method to download a file asynchronously from web server.

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;

                int 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)
    {
    }
}

这里有一个类似的主题-如何使用httpwebrequest下载文件

There is a similar thread here - how to download the file using httpwebrequest

这篇关于如何使用HttpWebRequest/Response从Web服务器下载二进制(.exe)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:50