【问题标题】:C# Downloading a zip file from url based on input paramsC#根据输入参数从url下载一个zip文件
【发布时间】:2013-01-23 08:13:51
【问题描述】:

我有一个要求,我必须根据输入参数使用 c#(大小可以在 10mb 到 400mb 之间变化)从服务器下载一个 zip 文件。例如,下载 userId = 10 和 year = 2012 的报告。
网络服务器接受这两个参数并返回一个 zip 文件。如何使用 WebClient 类来实现这一点?
谢谢

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 对于那个大小,我会使用 httpwebrequest/socket 来实现下载简历等功能
  • Stackoverflow 不是“给我代码”类型的网站。您应该自己研究并在遇到问题时提出问题。
  • 我要问的第一个问题是什么是 QueryString 参数什么是基于 Baris 答案的 URL如果我的理解有误,应该根据正确的参数来构建.. 像这样的工作Uri url = new Uri("http://www.microsoft.com/windows8.zip"). AddQuery("userid", @someUserId). AddQuery("year", @someyear);

标签: c# download http-post


【解决方案1】:

您可以通过扩展 WebClient 类来做到这一点

class ExtWebClient : WebClient
    {

        public NameValueCollection PostParam { get; set; }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest tmprequest = base.GetWebRequest(address);

            HttpWebRequest request = tmprequest as HttpWebRequest;

            if (request != null && PostParam != null && PostParam.Count > 0)
            {
                StringBuilder postBuilder = new StringBuilder();
                request.Method = "POST";
                //build the post string

                for (int i = 0; i < PostParam.Count; i++)
                {
                    postBuilder.AppendFormat("{0}={1}", Uri.EscapeDataString(PostParam.GetKey(i)),
                                             Uri.EscapeDataString(PostParam.Get(i)));
                    if (i < PostParam.Count - 1)
                    {
                        postBuilder.Append("&");
                    }
                }
                byte[] postBytes = Encoding.ASCII.GetBytes(postBuilder.ToString());
                request.ContentLength = postBytes.Length;
                request.ContentType = "application/x-www-form-urlencoded";

                var stream = request.GetRequestStream();
                stream.Write(postBytes, 0, postBytes.Length);
                stream.Close();
                stream.Dispose();

            }

            return tmprequest;
        }
    }

用法:如果你必须创建 POST 类型的请求

class Program
{
    private static void Main()
    {
        ExtWebClient webclient = new ExtWebClient();
        webclient.PostParam = new NameValueCollection();
        webclient.PostParam["param1"] = "value1";
        webclient.PostParam["param2"] = "value2";

        webclient.DownloadFile("http://www.example.com/myfile.zip", @"C:\myfile.zip");
    }
}

用法:对于GET类型的请求,你可以简单地使用普通的webclient

class Program
{
    private static void Main()
    {
        WebClient webclient = new WebClient();

        webclient.DownloadFile("http://www.example.com/myfile.zip?param1=value1&param2=value2", @"C:\myfile.zip");
    }
}

【讨论】:

  • 非常感谢,您的解决方案完美运行。在使用 HttpWebrequest 时,我能够下载流但无法将其保存在文件中。在使用 webclient 时,我无法将请求类型设置为 Post。您的解决方案结合了两者。
  • 你救了我的命!谢谢!
【解决方案2】:
string url = @"http://www.microsoft.com/windows8.zip";

WebClient client = new WebClient();

    client.DownloadFileCompleted +=    new AsyncCompletedEventHandler(client_DownloadFileCompleted);

    client.DownloadFileAsync(new Uri(url), @"c:\windows\windows8.zip");


void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("File downloaded");
}

【讨论】:

  • 他需要通过post参数下载文件,默认是GET
  • 哦,好的。他必须创建一个报告页面等,该页面接受参数然后返回相关文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
相关资源
最近更新 更多