C#模拟form表单提交数据,上传文件
2014-09-16 14:52
706 查看
一般的form数据提交
//一般form的数据提交
//数据的格式
string postData = "abc=这里我该怎么写呢&rd=" + DateTime.Now.Ticks;
byte[] postD = Encoding.UTF8.GetBytes(postData);
string slt = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://wechat.tsingming.test/bind/GetImage");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//以下注释可有可无
//request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
//request.KeepAlive = true;
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
Stream requestStream = request.GetRequestStream();
requestStream.Write(postD, 0, postD.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
slt = sr.ReadToEnd();
return Content(slt);
/**************************************************************/
//接收的方法
string abc = Request["abc"];
提交文件的form表单
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("http://localhost:8080/upload.aspx");
webReq.Method = "POST";
//webReq.ContentType = "application/x-www-form-urlencoded";
webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webReq.KeepAlive = true;
webReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
string boundary = "----" + DateTime.Now.Ticks.ToString("x");
string formdataTemplate = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
string dataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
var formdata = string.Format(formdataTemplate, "Filedata", name /*Path.GetFileName(fileName)*/);
webReq.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
//文件头
var formdataByte = Encoding.ASCII.GetBytes(formdata);
//尾
var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
Stream requestStream = webReq.GetRequestStream();
requestStream.Write(formdataByte, 0, formdataByte.Length);//写入 文件头
requestStream.Write(streamByte, 0, streamByte.Length);//写入 文件
var dataByte1 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "type", "memberHead"));
requestStream.Write(dataByte1, 0, dataByte1.Length);//写入 参数1
var dataByte2 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "tid", 48650));
requestStream.Write(dataByte2, 0, dataByte2.Length);//写入 参数2
var dataByte3 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "uid", 48650));
requestStream.Write(dataByte3, 0, dataByte3.Length);//写入 参数3
var dataByte4 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "Filename", name));
requestStream.Write(dataByte4, 0, dataByte4.Length);//写入 参数4
var dataByte5 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "Upload", "Submit Query"));
requestStream.Write(dataByte5, 0, dataByte5.Length);//写入 参数5
requestStream.Write(footer, 0, footer.Length);//写入 尾
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
slt.Message = sr.ReadToEnd();
sr.Close();
response.Close();
获得传送的文件
HttpPostedFileBase jpeg_image_upload = Request.Files["Filedata"];
注意:文件提交的表单格式(既:Request Payload)
Content-Type:multipart/form-data; boundary=----------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Request Payload
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="Filename"
Chrysanthemum.jpg
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="uid"
48650
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="type"
memberHead
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="Filedata"; filename="Chrysanthemum.jpg"
Content-Type: application/octet-stream
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="Upload"
Submit Query
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6--
数据格式很重。。。。
//一般form的数据提交
//数据的格式
string postData = "abc=这里我该怎么写呢&rd=" + DateTime.Now.Ticks;
byte[] postD = Encoding.UTF8.GetBytes(postData);
string slt = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://wechat.tsingming.test/bind/GetImage");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//以下注释可有可无
//request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
//request.KeepAlive = true;
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
Stream requestStream = request.GetRequestStream();
requestStream.Write(postD, 0, postD.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
slt = sr.ReadToEnd();
return Content(slt);
/**************************************************************/
//接收的方法
string abc = Request["abc"];
提交文件的form表单
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("http://localhost:8080/upload.aspx");
webReq.Method = "POST";
//webReq.ContentType = "application/x-www-form-urlencoded";
webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webReq.KeepAlive = true;
webReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
string boundary = "----" + DateTime.Now.Ticks.ToString("x");
string formdataTemplate = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
string dataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
var formdata = string.Format(formdataTemplate, "Filedata", name /*Path.GetFileName(fileName)*/);
webReq.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
//文件头
var formdataByte = Encoding.ASCII.GetBytes(formdata);
//尾
var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
Stream requestStream = webReq.GetRequestStream();
requestStream.Write(formdataByte, 0, formdataByte.Length);//写入 文件头
requestStream.Write(streamByte, 0, streamByte.Length);//写入 文件
var dataByte1 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "type", "memberHead"));
requestStream.Write(dataByte1, 0, dataByte1.Length);//写入 参数1
var dataByte2 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "tid", 48650));
requestStream.Write(dataByte2, 0, dataByte2.Length);//写入 参数2
var dataByte3 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "uid", 48650));
requestStream.Write(dataByte3, 0, dataByte3.Length);//写入 参数3
var dataByte4 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "Filename", name));
requestStream.Write(dataByte4, 0, dataByte4.Length);//写入 参数4
var dataByte5 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "Upload", "Submit Query"));
requestStream.Write(dataByte5, 0, dataByte5.Length);//写入 参数5
requestStream.Write(footer, 0, footer.Length);//写入 尾
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
slt.Message = sr.ReadToEnd();
sr.Close();
response.Close();
获得传送的文件
HttpPostedFileBase jpeg_image_upload = Request.Files["Filedata"];
注意:文件提交的表单格式(既:Request Payload)
Content-Type:multipart/form-data; boundary=----------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Request Payload
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="Filename"
Chrysanthemum.jpg
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="uid"
48650
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="type"
memberHead
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="Filedata"; filename="Chrysanthemum.jpg"
Content-Type: application/octet-stream
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6
Content-Disposition: form-data; name="Upload"
Submit Query
------------cH2cH2GI3KM7gL6ae0GI3ae0gL6gL6--
数据格式很重。。。。
相关文章推荐
- c# 模拟表单提交,post form 上传文件、大数据内容
- 用java模拟html表单(form)提交数据包含多文件上传表单提交。
- c# 模拟表单提交,post form 上传文件、大数据内容
- 上传文件 使用HTML5的FormData 提交表单和文件数据
- 使用Javascript XMLHttpRequest模拟表单(Form)提交上传文件
- Asp.Net模拟表单提交数据和上传文件的实现代码
- django(7)modelform操作及验证、ajax操作普通表单数据提交、文件上传、富文本框基本使用
- C#模拟web请求,使用post提交带有文件的数据表单
- Asp.Net模拟表单提交数据和上传文件的实现代码
- Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form me
- HttpClient:模拟表单提交文件,发送multipart/form-data数据
- Ajax模拟Form表单提交,含多种数据上传
- jsp form表单提交获取返回数据(也可用来上传文件、图片)
- C# 模拟提交 Form表单的数据
- js_ajax模拟form表单提交_多文件上传_支持单个删除
- 使用java ssm框架后台模拟post上传文件并提交表单数据
- httpurlconnection模拟post提交form表单(普通文本和上传文件) (
- c# 模拟提交带文件上传的表单
- C#实现使用form上传Excel文件,导入Excel数据到SQL SERVER数据库
- 模拟提交有文件上传的表单(通过http模拟上传文件)