关于HttpWebRequest上传文件
我们web 操作离不开 http请求响应 HttpWebRequest上传文件也是一样的道理
下面码一些代码:
private void UploadFile(string strRequestUri, string strCookie, string filename)
{
// 初始化HttpWebRequest
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strRequestUri); // 封装Cookie
Uri uri = new Uri(strRequestUri);
Cookie cookie = new Cookie("Name", strCookie);
CookieContainer cookies = new CookieContainer();
cookies.Add(uri, cookie);
httpRequest.CookieContainer = cookies; // 生成时间戳
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes(string.Format("\r\n--{0}--\r\n", strBoundary)); // 填报文类型
httpRequest.Method = "Post";
httpRequest.Timeout = * ;
httpRequest.ContentType = "multipart/form-data; boundary=" + strBoundary; // 封装HTTP报文头的流
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append(Environment.NewLine);
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file");
sb.Append("\"; filename=\"");
sb.Append(filename);
sb.Append("\"");
sb.Append(Environment.NewLine);
sb.Append("Content-Type: ");
sb.Append("multipart/form-data;");
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString()); // 计算报文长度
long length = postHeaderBytes.Length + this.FileUpload1.PostedFile.InputStream.Length + boundaryBytes.Length;
httpRequest.ContentLength = length; // 将报文头写入流
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(postHeaderBytes, , postHeaderBytes.Length); // 将上传文件内容写入流 //每次上传4k 1024*4
byte[] buffer = new Byte[checked((uint)Math.Min(, (int)this.FileUpload1.PostedFile.InputStream.Length))];
int bytesRead = ; while ((bytesRead = this.FileUpload1.PostedFile.InputStream.Read(buffer, , buffer.Length)) != )
{
requestStream.Write(buffer, , bytesRead);
} // 将报文尾部写入流
requestStream.Write(boundaryBytes, , boundaryBytes.Length);
// 关闭流
requestStream.Close();
} protected void btnRelease_Click(object sender, EventArgs e)
{
this.UploadFile(@"http://localhost/Qpdfgesigntest/SystemManager/WebsitePublishing/test.aspx", "yangtest", this.FileUpload1.PostedFile.FileName);
}
public partial class SystemManager_WebsitePublishing_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count > )
{
try
{
HttpPostedFile file = Request.Files[];
string filePath = "D:\\test\\" + file.FileName;
file.SaveAs(filePath); }
catch
{ }
}
}
}
关于HttpWebRequest上传文件的更多相关文章
- HTTPWebrequest上传文件--Upload files with HTTPWebrequest (multipart/form-data)
使用HTTPWebrequest上传文件遇到问题,可以参考Upload files with HTTPWebrequest (multipart/form-data)来解决 https://stack ...
- HttpWebRequest上传文件(Excel等)
//上传代码/// <summary> /// 文件上传 /// </summary> /// <param name="strAddress"> ...
- C#在WinForm下使用HttpWebRequest上传文件
转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...
- [转]C#在WinForm下使用HttpWebRequest上传文件并显示进度
/// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <param name=&qu ...
- C# 使用HttpWebRequest通过PHP接口 上传文件
1:上传文件实例 public void UploadXMLLog(string xmlpath) { NameValueCollection nvc = ne ...
- ASP.NET上传文件到远程服务器(HttpWebRequest)
/// <summary> /// 文件上传至远程服务器 /// </summary> /// <param name="url">远程服务地址 ...
- C#使用HttpWebRequest和HttpWebResponse上传文件示例
C#使用HttpWebRequest和HttpWebResponse上传文件示例 1.HttpHelper类: 复制内容到剪贴板程序代码 using System;using System.Colle ...
- HttpWebRequest上传多文件和多参数——整理
1.整理HttpWebRequest上传多文件和多参数.较上一个版本,更具普适性和简易型.注意(服务方web.config中要配置)这样就可以上传大文件了 <system.webServer&g ...
- ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合
一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...
随机推荐
- 2.2 Apache Axis2 快速学习手册之 AXIOM 构建 Web Service
和上一篇的POJO 部署相比主要是services.xml 中配置的消息接受处理器类不一样和Java 类中写法不一样. 使用AXIOM构建服务 样例源码路径: C:\Apps\axis2\axis2- ...
- Xilinx FPGA用户约束文件(转自xilinx ISE 开发指南
FPGA设计中的约束文件有3类:用户设计文件(.UCF文件).网表约束文件(.NCF文件)以及物理约束文件(.PCF文件),可以完成时序约束.管 脚约束以及区域约束.3类约束文件的关系为:用户在设计输 ...
- .NET MVC+ EF+LINQ 多表联查VIEW显示列表
1.VIEW 页面显示代码 <link href="~/Content/bootstrap.css" rel="stylesheet" /> < ...
- (转)在Docker中运行Java:为了防止失败,你需要知道这些
转自:https://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=2649693848&idx=1&sn=4e9ef7e2a9d ...
- 在python3.3后urllib2已经不能再用,只能用urllib.request来代替
版权声明:本文为博主原创文章,未经博主允许不得转载. 在python3.3后urllib2已经不能再用,只能用urllib.request来代替 response=urllib2.urlopen(' ...
- 分类算法----k近邻算法
K最近邻(k-Nearest Neighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一.该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的 ...
- uboot第一阶段关键位置分析
/* * the actual reset code */ reset: /* * set the cpu to SVC32 mode */ mrs r0,cpsr bic r0,r0,#0x1f o ...
- C#学习笔记(7)——委托
说明(2017-5-29 22:22:50): 1. 语法:public delegate void mydel();这一句在类外面,命名空间里面. 2. 专门新建一个方法,参数是委托: public ...
- ARKit从入门到精通(3)-ARKit自定义实现
转载:http://blog.csdn.net/u013263917/article/details/73038566 在上一小节中ARKit从入门到精通(2)-ARKit工作原理及流程介绍,我们完整 ...
- java基础篇---I/O技术(二)
接着上篇http://www.cnblogs.com/oumyye/p/4314412.html java I/O流---内存操作流 ByteArrayInputStream和ByteArrayOut ...