我现在有这样一个需求:在C#服务中(不是asp.net)向一个php页面用Post方法同时提交几个参数和一个图片。
我写的代码如下:Stream oStream;
string postData;
HttpWebRequest myRequest;
BinaryReader oReader;string imgWebsite = @"http://192.168.169.11/service/deal_image_service.php";//前面几个参数
postData = "command=UPLOAD";
postData += "&image_name=title_canyin.jpg";
postData += "&image_type=0" ;
postData += "&poi_id=182881";
postData += "&temp_id=3";
postData += "&file="; //file参数是图片内容
byte[] pardata = Encoding.UTF8.GetBytes(postData);//图片
oStream = new FileStream(@"D:\DotNetNuke_Install\Upload\title_canyin.jpg", FileMode.Open, FileAccess.Read);
oReader = new BinaryReader(oStream);
byte[] imgdata = oReader.ReadBytes(Convert.ToInt32(oStream.Length));//把两个byte数组连起来,也就是说file参数后面跟着图片的字节
byte[] data = new byte[postData.Length + imgdata.Length];
pardata.CopyTo(data, 0);
imgdata.CopyTo(data, pardata.Length);//开始发送
myRequest = (HttpWebRequest)WebRequest.Create(imgWebsite);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();//接受response内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string str = reader.ReadToEnd();
我这样做的结果是那边能接收到几个参数,但是最后的图片错误。是不是这样做的话,php把最后一个file也当成字符串参数了?怎么样才能像网页一样,同时提交 几个文本框 + 一个上传文件 ?
我写的代码如下:Stream oStream;
string postData;
HttpWebRequest myRequest;
BinaryReader oReader;string imgWebsite = @"http://192.168.169.11/service/deal_image_service.php";//前面几个参数
postData = "command=UPLOAD";
postData += "&image_name=title_canyin.jpg";
postData += "&image_type=0" ;
postData += "&poi_id=182881";
postData += "&temp_id=3";
postData += "&file="; //file参数是图片内容
byte[] pardata = Encoding.UTF8.GetBytes(postData);//图片
oStream = new FileStream(@"D:\DotNetNuke_Install\Upload\title_canyin.jpg", FileMode.Open, FileAccess.Read);
oReader = new BinaryReader(oStream);
byte[] imgdata = oReader.ReadBytes(Convert.ToInt32(oStream.Length));//把两个byte数组连起来,也就是说file参数后面跟着图片的字节
byte[] data = new byte[postData.Length + imgdata.Length];
pardata.CopyTo(data, 0);
imgdata.CopyTo(data, pardata.Length);//开始发送
myRequest = (HttpWebRequest)WebRequest.Create(imgWebsite);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();//接受response内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string str = reader.ReadToEnd();
我这样做的结果是那边能接收到几个参数,但是最后的图片错误。是不是这样做的话,php把最后一个file也当成字符串参数了?怎么样才能像网页一样,同时提交 几个文本框 + 一个上传文件 ?
解决方案 »
- .net C#如何发送qq,msn消息
- 如何让 protected internal 跨程序集!
- 【oj每周推荐】谈谈.Net里的引用
- 關於gridview控件的小問題<< 新手求助!!!請大大來幫忙!!!>>
- 怎么才能实现右键电脑中的文件时,在弹出的菜单中有自己写的程序呢。
- 用OLEDB打开excel文件遇到的问题?????????
- 如何向一个页面发送数据包,就好像form的post一样
- File.Copy覆盖一个文件后,想再一次覆盖,就提示资源已经打开
- help me!
- 在web编程中怎么与数据库连不上?有人出现过这样的问题妈?着急死了,如果不行我以后的工作就没发作了。
- 寻找常用菜单图标
- 关于ref
包头
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-silverlight, */*
Referer: http://localhost:1099/WebSite2/Default.aspx
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7d81f43530558
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MathPlayer 2.0; .NET CLR 2.0.50727)
Host: localhost:1099
Content-Length: 969
Proxy-Connection: Keep-Alive
Pragma: no-cache
Cookie: user=checked=no
Request Body里的-----------------------------7d81f43530558
Content-Disposition: form-data; name="__VIEWSTATE"/wEPDwUKLTQwMjY2MDA0Mw9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YWRk8P+TRFkYQ6Jyhr55Y4q8ZC/PwqE=
-----------------------------7d81f43530558
Content-Disposition: form-data; name="FileUpload1"; filename="C:\Documents and Settings\ly\桌面\1.html"
Content-Type: text/html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<head> </head>
<body>
<input type='text' onkeypress="if(event.keyCode >= 48 && event.keyCode <= 57) return false;" />
</body> </html>
-----------------------------7d81f43530558
Content-Disposition: form-data; name="Button1"Button
-----------------------------7d81f43530558
Content-Disposition: form-data; name="__EVENTVALIDATION"/wEWAgKM2f7ICQKM54rGBrMsejOnlUqruOgzsZsxQACwccHs
-----------------------------7d81f43530558--
如果不是,你可以询问php接收post数据的协议(格式),或者下载一个抓包程序,通过IE等方式查看其自己通讯时的数据格式.
如是是的,你可以在php页面的responsed方法中定义一个数据格式,比如bytes的前几个字节代表一共一个图片,然后跟的是图片大小等等.winform post时按照此协议打包发送即可.不知解释的你是否能明白?呵呵