winform 下载网络资源

调用

						//网络地址                      文件的名字                存放本地路径
DownLoadFile.HttpDownload(@"http://39.127.0.1:8092/" + readerppp.GetString(2), "./img/" + readerppp.GetString(2));

工具

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;


namespace TextWinform1
{
    class DownLoadFile
    {
        /// <summary>
        /// 专门的下载文件类
        /// </summary>
      
            /// <summary>
            /// http下载文件
            /// </summary>
            /// <param name="url"></param>
            /// <param name="path"></param>
            /// </summary>
            /// <param name="url">下载文件地址</param>
            /// <param name="path">文件存放地址,包含文件名</param>
            /// <param name="progressBar1"></param>
            /// <param name="label_Info">提示信息 ,时时提示下载多少K的 </param>
            /// <returns></returns>
            public static bool HttpDownload(string url, string path)
            {
                string tempPath = System.IO.Path.GetDirectoryName(path);
                if (!System.IO.Directory.Exists(tempPath))
                    System.IO.Directory.CreateDirectory(tempPath); 
                    string tempFile = tempPath + "/" + System.IO.Path.GetFileName(path); 
                try
                {
                    FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                    // 设置参数
                    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                    //发送请求并获取相应回应数据
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    Stream responseStream = response.GetResponseStream();
                    //创建本地文件写入流
                    //Stream stream = new FileStream(tempFile, FileMode.Create);
                    byte[] bArr = new byte[1024];
                    int size = responseStream.Read(bArr, 0, (int)bArr.Length);


                    if (response.ContentLength != 0)
                    {
                        if (request.ContentLength > int.MaxValue)
                        {
                            MessageBox.Show("要下载的文件太大,超出范围!");
                            return false;
                        }

                    }

                    while (size > 0)
                    {
                        //stream.Write(bArr, 0, size);
                        fs.Write(bArr, 0, size);
                        size = responseStream.Read(bArr, 0, (int)bArr.Length);

                        /*progressBar1.Invoke(new Action(() => {
                            progressBar1.Value += size;
                        }));*/
                    }
                    fs.Close();
                    responseStream.Close();
                //if (System.IO.File.Exists(path))
                //{
                //    System.IO.File.Delete(path);    //存在则删除
                //}
                //System.IO.File.Move(tempFile, path);

                //if (System.IO.File.Exists(tempFile))
                //{
                //    System.IO.File.Delete(tempFile);    //存在则删除
                //}
                return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }

            delegate void SetTextCallBack(string text, Label txt_a);
            private static void SetText(string text, Label txt_a)
            {
                if (txt_a.InvokeRequired)
                {
                    SetTextCallBack stcb = new SetTextCallBack(SetText);
                    txt_a.Invoke(stcb, new object[] { text, txt_a });
                }
                else
                {
                    txt_a.Text = text;
                }
            }



            public static string SendGetRequest(string url)
            {
                try
                {

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "get";

                    //if (CookiesContainer == null)
                    //{
                    //    CookiesContainer = new CookieContainer();
                    //}

                    //request.CookieContainer = CookiesContainer;  //启用cookie

                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream response_stream = response.GetResponseStream();

                    int count = (int)response.ContentLength;
                    int offset = 0;
                    byte[] buf = new byte[count];
                    while (count > 0)  //读取返回数据
                    {
                        int n = response_stream.Read(buf, offset, count);
                        if (n == 0) break;
                        count -= n;
                        offset += n;
                    }
                    return Encoding.UTF8.GetString(buf);
                }
                catch
                {
                    return null;
                }
            }

        }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值