zoukankan      html  css  js  c++  java
  • C# http post上传文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Threading;
    
    namespace HttpUploadTest
    {
        class Program
        {
            //static string m_address = "http://localhost:50000";
            static string m_address = "http://210.73.221.45:50000";
            static void FileUpload(object m_fileNamePath)
            {
                DateTime start = DateTime.Now;
              
                  // 要上传的文件 
                FileStream oFileStream = new FileStream(m_fileNamePath.ToString(), FileMode.Open, FileAccess.Read);
                BinaryReader oBinaryReader = new BinaryReader(oFileStream);
    
                // 根据uri创建HttpWebRequest对象 
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(m_address));
                httpReq.Method = "POST";
    
                //对发送的数据不使用缓存 
                httpReq.AllowWriteStreamBuffering = false;
    
                //设置获得响应的超时时间(半小时) 
                httpReq.Timeout = 300000;
               //httpReq.Timeout = 5000;
               // httpReq.ReadWriteTimeout = 150000;
                httpReq.KeepAlive = true;
                httpReq.ProtocolVersion = HttpVersion.Version11;
    
                httpReq.ContentType = "application/pdf";
                long filelength = oFileStream.Length;
                httpReq.SendChunked = true;
                //在将 AllowWriteStreamBuffering 设置为 false 的情况下执行写操作时,必须将 ContentLength 设置为非负数,或者将 SendChunked 设置为 true。
            
                //每次上传4k 
                int bufferLength = 4*1024;
                byte[] buffer = new byte[bufferLength];
    
                //已上传的字节数 
                long offset = 0;
    
                //开始上传时间 
                DateTime startTime = DateTime.Now;
                int size = oBinaryReader.Read(buffer, 0, bufferLength);
                Stream postStream = httpReq.GetRequestStream();
    
                while (size > 0)
                {
                    postStream.Write(buffer, 0, size);
                    offset += size;
                    size = oBinaryReader.Read(buffer, 0, bufferLength);
                    //Console.Write(".");
                }
                //Console.WriteLine(".");
                postStream.Flush();
                postStream.Close();
    
                //获取服务器端的响应 
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                DateTime end = DateTime.Now;
                TimeSpan ts = end - start;
                //读取服务器端返回的消息 
                String sReturnString = sr.ReadLine();
                Console.WriteLine("retcode="+sReturnString+" 花费时间="+ts.TotalSeconds.ToString());
                s.Close();
                sr.Close();
            }
    
            static void Main(string[] args)
            {
                string filePath = "D:\test";
                string[] ary = Directory.GetFiles(filePath);
                for (int i = 0; i < (int)ary.Count(); i++)
                {
                    Thread oneProcess = new Thread(new ParameterizedThreadStart(FileUpload));
                    oneProcess.Priority = ThreadPriority.AboveNormal;
                    oneProcess.Start(ary[i]);
                    oneProcess.IsBackground = false;
                }
            }
        }
    }
    

      

  • 相关阅读:
    .Net魔法堂:史上最全的ActiveX开发教程——发布篇
    .Net魔法堂:史上最全的ActiveX开发教程——开发篇
    JS魔法堂:浏览器模式和文档模式怎么玩?
    JS魔法堂:精确判断IE的文档模式by特征嗅探
    JS魔法堂:追忆那些原始的选择器
    意译:自调用函数表达式
    一起Polyfill系列:让Date识别ISO 8601日期时间格式
    一起Polyfill系列:Function.prototype.bind的四个阶段
    poco 线程库
    CDN理解<转>
  • 原文地址:https://www.cnblogs.com/zendu/p/7884139.html
Copyright © 2011-2022 走看看