zoukankan      html  css  js  c++  java
  • 在Windows Mobile 中利用 WebRequest 下载文件并获得响应头的信息信息

    点击此处下载源代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;

    namespace DownloadFile
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void btnGet_Click(object sender, EventArgs e)
            {
                Uri u = new Uri(txtUrl.Text);
                Download(u, System.Environment.GetFolderPath(Environment.SpecialFolder.Personal)/* 此处获得我的文档的路径 */);
     
            }

            private void Download(Uri address, string localPath)
            {
                string filename = "test.txt";
                WebRequest request = WebRequest.Create(address);

                //perform the GET request
                WebResponse response = request.GetResponse();

                //get stream containing received data
                Stream s = response.GetResponseStream();

                //open filestream for the output file
                FileStream fs = new FileStream(Path.Combine(localPath, filename), FileMode.Create, FileAccess.Write);

                //copy until all data is read 标准的缓存读取格式
                byte[] buffer = new byte[1024];
                int bytesRead = s.Read(buffer, 0, buffer.Length);
                while (bytesRead > 0)
                {
                    fs.Write(buffer, 0, bytesRead);
                    bytesRead = s.Read(buffer, 0, buffer.Length);
                }

                //close both streams
                fs.Close();
                s.Close();
                response.Close();

                MessageBox.Show("ok");
            }

            private void btnHead_Click(object sender, EventArgs e)
            {
             
                System.Net.HttpWebRequest hwr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(txtUrl.Text);
                hwr.Method = "HEAD";
                System.Net.WebResponse response = hwr.GetResponse();
                string[] str = response.Headers.AllKeys;
                foreach (string temp in str)
                {
                    this.listBox1.Items.Add("key="+temp+" length="+response.Headers[temp]);
                }
            }

            private void menuItem1_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
        }
    }

  • 相关阅读:
    编写你的应用程序(二)、原生客户端模块
    编写你的应用程序(一)、应用结构
    checkpoint机制,show engine innodb status
    InnoDB关键特性,innodb_old_blocks_time,锁,内存管理,latch争用
    Innodb引擎,MySQL修改参数
    MySQL数据库体系结构
    IT行业数据库分析
    生成一个千万行的表
    范式小知识
    MySQL触发器
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/1579033.html
Copyright © 2011-2022 走看看