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;
}
}
}
}
C# http post上传文件的更多相关文章
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader
发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...
- asp.net mvc 上传文件
转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...
- app端上传文件至服务器后台,web端上传文件存储到服务器
1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...
- .net FTP上传文件
FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- 前端之web上传文件的方式
前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...
- Django session cookie 上传文件、详解
session 在这里先说session 配置URL from django.conf.urls import patterns, include, url from django.contrib i ...
- 4 django系列之HTML通过form标签来同时提交表单内容与上传文件
preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...
- 1. Django系列之Django与ajax上传文件
html代码如下: <div class="form-group"> <label for="exampleInputFile">附件上 ...
随机推荐
- cogs 539. 牛棚的灯
★★☆ 输入文件:lights.in 输出文件:lights.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从 ...
- storm(一) window机制
Watermark作用 在解释storm的window之前先说明一下watermark原理. Watermark中文翻译为水位线更为恰当. 顺序的数据从源头开始发送到到操作,中间过程肯定会出现数据乱序 ...
- windows下Redis主从复制配置(报错:Invalid argument during startup: unknown conf file parameter : slaveof)
主从复制配置中的遇到的异常: Invalid argument during startup: unknown conf file parameter : slaveof 把Redis文件夹复制两份 ...
- MongoDB 性能优化
Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...
- NPM Scripts 2 -- rimraf copyfiles imagemin usemin htmlmin uglifyjs
NPM Scripts Part 2 Objectives and Outcomes In this exercise you will learn to build a distribution f ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- Python操作Memcached使用Python-memcached模块
安装Python的memcached驱动模块 pip install python-memcached 简单的操作示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/ ...
- 安装 inotify-tools
摘要 inotify-tools, 是一款google出的用于监控文件系统的软件. 一.软件下载地址官方站点地址:http://inotify-tools.sourceforge.net/仓库地址:h ...
- python+opencv+pyqt5控制摄像头在Qlabel上显示
import cv2 import numpy as numpy from PIL import * import sys from PyQt5.QtWidgets import * from PyQ ...
- python学习大纲目录(转自alex博客https://www.cnblogs.com/alex3714/)
day01: 介绍.基本语法.流程控制 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 ...