C#模拟form表单提交数据,上传文件
一般的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 + "
Content-Disposition: form-data; name="{0}"; filename="{1}"
Content-Type: application/octet-stream
";
string dataTemplate = "
--" + boundary + "
Content-Disposition: form-data; name="{0}"
{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("
--" + boundary + "--
");
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--
数据格式很重。。。。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
