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

      

  • 相关阅读:
    HDU4529 郑厂长系列故事——N骑士问题 —— 状压DP
    POJ1185 炮兵阵地 —— 状压DP
    BZOJ1415 聪聪和可可 —— 期望 记忆化搜索
    TopCoder SRM420 Div1 RedIsGood —— 期望
    LightOJ
    LightOJ
    后缀数组小结
    URAL
    POJ3581 Sequence —— 后缀数组
    hdu 5269 ZYB loves Xor I
  • 原文地址:https://www.cnblogs.com/zendu/p/7884139.html
Copyright © 2011-2022 走看看