我们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上传文件的更多相关文章

  1. HTTPWebrequest上传文件--Upload files with HTTPWebrequest (multipart/form-data)

    使用HTTPWebrequest上传文件遇到问题,可以参考Upload files with HTTPWebrequest (multipart/form-data)来解决 https://stack ...

  2. HttpWebRequest上传文件(Excel等)

    //上传代码/// <summary> /// 文件上传 /// </summary> /// <param name="strAddress"> ...

  3. C#在WinForm下使用HttpWebRequest上传文件

    转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...

  4. [转]C#在WinForm下使用HttpWebRequest上传文件并显示进度

    /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <param name=&qu ...

  5. C# 使用HttpWebRequest通过PHP接口 上传文件

    1:上传文件实例 public void UploadXMLLog(string xmlpath)         {             NameValueCollection nvc = ne ...

  6. ASP.NET上传文件到远程服务器(HttpWebRequest)

    /// <summary> /// 文件上传至远程服务器 /// </summary> /// <param name="url">远程服务地址 ...

  7. C#使用HttpWebRequest和HttpWebResponse上传文件示例

    C#使用HttpWebRequest和HttpWebResponse上传文件示例 1.HttpHelper类: 复制内容到剪贴板程序代码 using System;using System.Colle ...

  8. HttpWebRequest上传多文件和多参数——整理

    1.整理HttpWebRequest上传多文件和多参数.较上一个版本,更具普适性和简易型.注意(服务方web.config中要配置)这样就可以上传大文件了 <system.webServer&g ...

  9. ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合

    一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...

随机推荐

  1. Spring Security教程(七):RememberMe功能

    在之前的教程中一笔带过式的讲了下RememberMe记住密码的功能,那篇的Remember功能是最简易的配置,其功能和安全性都不强.这里就配置下security中RememberMe的各种方式. 一. ...

  2. activiti主要API对象

    ProcessEngine         //流程引擎 RuntimeService      //与流程实例,执行对象(正在执行) TaskService           //正在执行任务 H ...

  3. [LeetCode] Read N Characters Given Read4 I & II

    Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...

  4. Android log 日志分析

    一. Log 日志中 Bug 类型 程序异常强制关闭: Force Close ,Fatal 程序无响应: Application Not Response , ANR(应用无响应).一般是主线程超时 ...

  5. JasperReport学习札记6-JRXML的标签

    原文源于:http://langhua9527.iteye.com/blog/402317 JasperReport学习笔记6-JRXML的标签1.<jasperReport>根元素包括很 ...

  6. 用sendcloud来发邮件

    平时发验证码邮件都是用免费域名邮箱,但是有时一频繁发多了就发不了了,听说用sendcloud可以避免,还能避免阿里云邮件发QQ邮箱进垃圾箱中,去注册了下,免费账户号每个月才50封,自己玩玩可以吧.. ...

  7. ASP.NET MVC中的cshtml页面中的下拉框的使用

    ASP.NET MVC中的cshtml页面中的下拉框的使用 用上@Html.DropDownList 先记下来..以做备忘...

  8. PostgreSQL 保存json,jsonb类型

    PostgresQL 字符串隐式转换JSON脚本: -- 隐式将varchar转换为json CREATE OR REPLACE FUNCTION json_in_varchar(varchar) R ...

  9. 【Unity】4.5 树木创建器

    分类:Unity.C#.VS2015 创建日期:2016-04-11 一.简介 在地形编辑器一节中,已经告诉了你如何使用已经创建好的树来形成大片树林.这一节告诉你在 Unity 5.3.4中如何利用[ ...

  10. GGGGCCCC

    Evaluating and improving remembered sets in the HotSpot G1 garbage collector http://www.diva-portal. ...