public static string HttpUploadFile(string url, string path)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + ); //请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, , bArr.Length);
fs.Close(); Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, , itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, , postHeaderBytes.Length);
postStream.Write(bArr, , bArr.Length);
postStream.Write(endBoundaryBytes, , endBoundaryBytes.Length);
postStream.Close(); //发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}

HttpWebRequest post上传文件的更多相关文章

  1. 使用HttpWebRequest POST上传文件

    2019/10/27, .Net c#代码片段 摘要:使用HttpWebRequest向Api接口发送文件,multipart-form数据格式,POST方式 参考地址 /// <summary ...

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

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

  3. HttpWebRequest上传文件(Excel等)

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

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

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

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

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

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

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

  7. 关于HttpWebRequest上传文件

    我们web 操作离不开 http请求响应 HttpWebRequest上传文件也是一样的道理 下面码一些代码: private void UploadFile(string strRequestUri ...

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

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

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

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

随机推荐

  1. selenium之 chromedriver与chrome版本映射表

    看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整理了一份如下,希望对大家有用: chromedriver版本 支持的Chrome版本 v2.40 v66-68 v2. ...

  2. 各种Java项目环境搭建-文档引用汇总记录

    springmvc环境搭建 1.如何用Maven创建web项目(具体步骤) 2.springmvc环境搭建,一步一步超简单

  3. C++获取Lua全局变量和执行Lua多参数多返回值函数

    C++代码: // LuaAndC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #i ...

  4. MyEclipse10.6/Eclipse4.2 集成 flash builder 4.6

    MyEclipse10.6/Eclipse4.2 集成 flash builder 4.61. 安装 Myeclipse10.6 或 Eclipse 4.2 以及 Flash builder 4.6 ...

  5. [Android]异步任务AsyncTask使用解析

    AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法.注意继承时需要设定三个泛型P ...

  6. Windows可信任路径代码执行漏洞

    乌云里有很多这样的案例,当然在开发过程中也会存在这样的问题 搜索:可信任路径漏洞 调用案例中的描述: Microsoft Windows API使用CreateProcess()函数创建新的进程及其主 ...

  7. Three.js响应和移动物体

    效果图 demo import './index.css'; // stats var stats; (function(){ stats = new Stats(); document.body.a ...

  8. Three.js加载gltf模型

    效果图 demo import './index.css'; var stats; stats = new Stats(); document.body.appendChild( stats.dom ...

  9. Python下OS模块重命名方法renames

    在python中有很多强大的模块,其中我们经常要使用的就是OS模块,OS模块提供了超过200个方法来供我们使用,并且这些方法都是和数据处理相关的,这里介绍下重命名这个方法. OS的重命名方法是os.r ...

  10. Protege4.3使用入门(二)

    1.支持OWLViz 利用OWLViz查看我们构建Class的结构图.如果尚未安装,请到http://www.graphviz.org/pub/graphviz/stable/windows/grap ...