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();
            }
        }
    }

  • 相关阅读:
    paip.解决Invalid byte 2 of 2byte UTF8 sequence.
    poj1157
    poj1258
    poj1160
    poj1113
    poj1159
    !!!GRETA正则表达式模板类库
    【原创】C#与C++的混合编程采用其中的第三种方法
    WinApi.cs
    C#:正则表达式30分钟入门教程
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/1579033.html
Copyright © 2011-2022 走看看