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

  

posted on 2017-11-23 11:55  zyz913614263  阅读(11412)  评论(0编辑  收藏  举报

导航