using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Specialized; namespace TechnetSamples
{
class Program
{
static void Main(string[] args)
{
string URLAuth = "https://technet.rapaport.com/HTTP/Authenticate.aspx";
WebClient webClient = new WebClient(); NameValueCollection formData = new NameValueCollection();
formData["Username"] = "myUser";
formData["Password"] = "myPassword"; byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
webClient.Dispose(); string URL = "http://technet.rapaport.com/HTTP/Upload/Upload.aspx?Method=file";
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(URL); webRequest.Method = "POST";
webRequest.ContentType = "multipart/form-data; boundary=" + boundary; string FilePath = "C:\\test.csv";
formData.Clear();
formData["ticket"] = resultAuthTicket;
formData["ReplaceAll"] = "false"; Stream postDataStream = GetPostStream(FilePath, formData, boundary); webRequest.ContentLength = postDataStream.Length;
Stream reqStream = webRequest.GetRequestStream(); postDataStream.Position = ; byte[] buffer = new byte[];
int bytesRead = ; while ((bytesRead = postDataStream.Read(buffer, , buffer.Length)) != )
{
reqStream.Write(buffer, , bytesRead);
} postDataStream.Close();
reqStream.Close(); StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
string Result = sr.ReadToEnd();
} private static Stream GetPostStream(string filePath, NameValueCollection formData, string boundary)
{
Stream postDataStream = new System.IO.MemoryStream(); //adding form data
string formDataHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
"Content-Disposition: form-data; name=\"{0}\";" + Environment.NewLine + Environment .NewLine + "{1}"; foreach (string key in formData.Keys)
{
byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(formDataHeaderTemplate,
key, formData[key]));
postDataStream.Write(formItemBytes, , formItemBytes.Length);
} //adding file data
FileInfo fileInfo = new FileInfo(filePath); string fileHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" +
Environment.NewLine + "Content-Type: application/vnd.ms-excel" + Environment.NewLine + Environment.NewLine; byte[] fileHeaderBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(fileHeaderTemplate,
"UploadCSVFile", fileInfo.FullName)); postDataStream.Write(fileHeaderBytes, , fileHeaderBytes.Length); FileStream fileStream = fileInfo.OpenRead(); byte[] buffer = new byte[]; int bytesRead = ; while ((bytesRead = fileStream.Read(buffer, , buffer.Length)) != )
{
postDataStream.Write(buffer, , bytesRead);
} fileStream.Close(); byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--");
postDataStream.Write(endBoundaryBytes, , endBoundaryBytes.Length); return postDataStream;
}
}
}

C#模拟HTTP Form请求上传文件的更多相关文章

  1. JAVA模拟HTTP post请求上传文件

    在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...

  2. SpringMVC实现PUT请求上传文件

    在JQuery中,我们可以进行REST ful中delete和put的请求,但是在java EE标准中,默认只有在POST请求的时候,servlet 才会通过getparameter()方法取得请求体 ...

  3. Postman Post请求上传文件

    Postman Post请求上传文件一.选择post请求方式,输入请求地址 二.填写Headers Key:Content-Type :Value:multipart/form-data 如下图 三. ...

  4. 前端 - jquery方式 / iframe +form 方式 上传文件

    环境与上一章一样 jquery 方式上传文件: HTML代码 {#html代码开始#} <input type="file" id="img" > ...

  5. SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)

    1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...

  6. python中使用multipart/form-data请求上传文件

    最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~ 接口例如: URL: http://www.baidu.com/*** method:post 参数: { "salar ...

  7. element-ui上传组件,通过自定义请求上传文件

    记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...

  8. Java模拟表单POST上传文件

    JAVA模拟表单POST上传文件 import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.io ...

  9. 通过PHP CURL模拟请求上传文件|图片。

    现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...

随机推荐

  1. Atitit.如何建立研发体系

    Atitit.如何建立研发体系 组织,流程,prj..Mana  oppm 发管理是一个完整的管理体系,从结构上来讲,它主要由四个方面的内容构架而成:组织结构与岗位设置 管理流程与工作流程..项目及管 ...

  2. CentOS:ECDSA host key "ip地址" for has changed and you have requested strict checking(转)

    原文地址:http://blog.csdn.net/ausboyue/article/details/52775281 Linux SSH命令错误:ECDSA host key "ip地址& ...

  3. CentOS 7 安装出现 /dev/root does not exits 导致无法安装的问题

    本人在官网下的是这个 CentOS-7-x86_64-DVD-1611.iso ,然后用UltraISO 9.6制作的U盘启动盘,不过在安装的时候出现了这个错误, 然后也是搜了好久,试了一下,下面这个 ...

  4. centos 6 安装配置openvpn

    下载地址:http://swupdate.openvpn.org/community/releases/http://www.oberhumer.com/opensource/lzo/download ...

  5. webService

    什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用.  1:从WebService的工作模式上 ...

  6. MEF学习

    一.   什么是MEF MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库. 应用程序开发人员可利用该库发现并使用扩展,而无需进行配置. 扩 ...

  7. 转 10 个最佳的 Node.js 的 MVC 框架

    10 个最佳的 Node.js 的 MVC 框架 oschina 发布于: 2014年02月24日 (33评) 分享到:    收藏 +322 Node.js 是一个基于Chrome JavaScri ...

  8. 大型网站的灵魂——性能

    前言     在前一篇随笔<大型网站系统架构的演化>中,介绍了大型网站的演化过程,期间穿插了一些技术和手段,我们可以从中看出一个大型网站的轮廓,但想要掌握设计开发维护大型网站的技术,需要我 ...

  9. android-解决全屏-webview-输入框被输入法挡住-FullScreen-adjustResize失效问题

    由于公司开发的 App 中,Html 的页面嵌入的有点多,坑爹的是,还有很多输入框,这就算了,还要求全屏.然后就出现了这个情况. 下面来唠叨唠叨具体的来龙去脉. 起初是这样的,整个项目基本完工了.测试 ...

  10. !+"\v1" 用来“判断浏览器类型”还是用来“IE判断版本”的问题!

    这种写法是利用各浏览器对转义字符"\v"的理解不同来判断浏览器类型.在IE中,"\v"没有转义,得到的结果为"v".而在其他浏览器中&quo ...