HttpWebRequest post "multipart/form-data"类型的web数据
2008-07-16 15:34
519 查看
webRqst.ContentType = "multipart/form-data; boundary=xyz";
所以数据提交部份实际上是从你的boundary也就是
-----------------------------7d42bf2030536开始到结束。
以下是一个例子:
System.Uri uri = new Uri(msp_url);
string dataBoundary = "--xyz";
System.Net.HttpWebRequest webRqst = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
//add existing cookies to the webrequest.
webRqst.CookieContainer = new CookieContainer();
webRqst.CookieContainer.Add(mspCookies);
//create the WebRequest Content-Body manually for file upload.
webRqst.ContentType = "multipart/form-data; boundary=xyz";
webRqst.Method = "POST";
webRqst.KeepAlive = true;
//get the file content that will be uploaded.
StreamReader sr = new StreamReader(filepath);
string FileData = sr.ReadToEnd(); // test data to send.
sr.Close();
//make a string to store the content.
StringBuilder DataString = new StringBuilder();
DataString.Append(dataBoundary + "/r/n");
DataString.Append("Content-Disposition: form-data; name=/"filename/"/r/n/r/n" + "test.txt/r/n");
DataString.Append(dataBoundary + "/r/n");
DataString.Append("Content-Disposition: form-data; name=" + "/"submitFile/"" +
"; filename=" + "/"" + filename + "/"" + "/r/n");
DataString.Append("Content-Type: text/plain/r/n/r/n");
DataString.Append(FileData);
DataString.Append(dataBoundary + "--/r/n");
byte []Postdata = System.Text.Encoding.Default.GetBytes(DataString.ToString());
//change the requests content length to the size of data being posted in request.
webRqst.ContentLength = Postdata.Length;
//get the response stream from the webrequest and write the data to be posted to the
//Request Stream
Stream tempStream = webRqst.GetRequestStream();
tempStream.Write(Postdata,0,Postdata.Length);
tempStream.Flush();
tempStream.Close();
//change certificate policy to always allow.
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
System.Net.HttpWebResponse webRsp = (System.Net.HttpWebResponse)webRqst.GetResponse();
[抄录]http://topic.csdn.net/t/20040621/10/3109172.html
所以数据提交部份实际上是从你的boundary也就是
-----------------------------7d42bf2030536开始到结束。
以下是一个例子:
System.Uri uri = new Uri(msp_url);
string dataBoundary = "--xyz";
System.Net.HttpWebRequest webRqst = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
//add existing cookies to the webrequest.
webRqst.CookieContainer = new CookieContainer();
webRqst.CookieContainer.Add(mspCookies);
//create the WebRequest Content-Body manually for file upload.
webRqst.ContentType = "multipart/form-data; boundary=xyz";
webRqst.Method = "POST";
webRqst.KeepAlive = true;
//get the file content that will be uploaded.
StreamReader sr = new StreamReader(filepath);
string FileData = sr.ReadToEnd(); // test data to send.
sr.Close();
//make a string to store the content.
StringBuilder DataString = new StringBuilder();
DataString.Append(dataBoundary + "/r/n");
DataString.Append("Content-Disposition: form-data; name=/"filename/"/r/n/r/n" + "test.txt/r/n");
DataString.Append(dataBoundary + "/r/n");
DataString.Append("Content-Disposition: form-data; name=" + "/"submitFile/"" +
"; filename=" + "/"" + filename + "/"" + "/r/n");
DataString.Append("Content-Type: text/plain/r/n/r/n");
DataString.Append(FileData);
DataString.Append(dataBoundary + "--/r/n");
byte []Postdata = System.Text.Encoding.Default.GetBytes(DataString.ToString());
//change the requests content length to the size of data being posted in request.
webRqst.ContentLength = Postdata.Length;
//get the response stream from the webrequest and write the data to be posted to the
//Request Stream
Stream tempStream = webRqst.GetRequestStream();
tempStream.Write(Postdata,0,Postdata.Length);
tempStream.Flush();
tempStream.Close();
//change certificate policy to always allow.
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
System.Net.HttpWebResponse webRsp = (System.Net.HttpWebResponse)webRqst.GetResponse();
[抄录]http://topic.csdn.net/t/20040621/10/3109172.html
相关文章推荐
- HTTP协议之post multipart/form-data数据类型实例
- C#中HttpWebRequest用Post提交Content-Type: multipart/form-data;
- C#中HttpWebRequest用Post提交Content-Type: multipart/form-data; boundary=
- spring mvc源码-》MultipartReques类-》主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multipart/form-data"格式,以二进制形式提交数据,提交方式为post方式。
- Http multipart/form-data多参数Post方式上传数据
- form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据
- post数据丢失问题 enctype="multipart/form-data"
- Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form me
- C#中使用HttpWebRequest用Post提交MultiPart数据
- 如何使用HttpWebRequest POST数据时设置Connection="Keep-Alive"
- 过滤器中处理multipart/form-data头部的post请求request.getParameter(")获取不到参数问题
- XMLHttpRequest 中的数据类型 - DOMString、Document、FormData、Blob、File、ArrayBuffer
- C#中使用HttpWebRequest用Post提交MultiPart数据
- C# 使用HttpWebRequest用Post提交MultiPart数据
- 使用HttpWebRequest用Post提交MultiPart数据
- struts2开发环境中,enctype="multipart/form-data"上传文件时,request.getInputStream()为null的原因(转http://topic.csdn.net/u/20101210/11/78454a19-7
- C# 使用HttpWebRequest用Post提交MultiPart数据
- struts中post方式上传文件,enctype="multipart/form-data",request.getParameter("file") 是null的问题分析
- C#中使用HttpWebRequest用Post提交MultiPart数据
- form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据