我现在有这样一个需求:在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也当成字符串参数了?怎么样才能像网页一样,同时提交 几个文本框 + 一个上传文件 ?

解决方案 »

  1.   

    写成跟下面一样就可以,自己慢慢模拟吧
    包头
    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--
      

  2.   

    向一个php页面用Post方法同时提交几个参数和一个图片----------------请问这个php页面是你做的吗?
    如果不是,你可以询问php接收post数据的协议(格式),或者下载一个抓包程序,通过IE等方式查看其自己通讯时的数据格式.
    如是是的,你可以在php页面的responsed方法中定义一个数据格式,比如bytes的前几个字节代表一共一个图片,然后跟的是图片大小等等.winform post时按照此协议打包发送即可.不知解释的你是否能明白?呵呵
      

  3.   

    php是别人做的,而且不是一个部门,态度也不友好,不愿意找他们。我用网络监视器看了,觉得很麻烦,就是想问有没有 现成的类可以简单实现的。我打算按照网络监视器中看到的格式拼byte数组了,然后发出去。