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

      

  • 相关阅读:
    Oracle 左连接、右连接、全外连接、(+)号作用
    ORA-01940:无法删除当前已链接的用户(转)
    博客正式开通
    python扫描内网存活主机(scapy与nmap模块)
    Python学习之扫描网段主机与开启端口(避坑)
    Python学习之服务器与客户端的交互
    python渗透测试编程之kali linux2的AptanaStudio3安装
    Shellcodeloader免杀过火绒
    Cracer之Waf绕过
    30 Day Challenge Day 16 | Leetcode 692. Top K Frequent Words
  • 原文地址:https://www.cnblogs.com/zendu/p/7884139.html
Copyright © 2011-2022 走看看