向服务器POST数据(参数+文件)
public string PostFile(string FilePath,string user,string codeid, Encoding encoding, ProgressBar p)
{
string strResult = string.Empty;
string param = "userid" + "=" + user + "&" +"code" + "=" + codeid + "&" + "Photo" + "=";
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(PostUrl);
myRequest.Method = "Post";
myRequest.KeepAlive = true;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Accept = "*/*";
byte[] PostArray = encoding.GetBytes(param);
FileStream Filestream = new FileInfo(FilePath).OpenRead();
BinaryReader oReader = new BinaryReader(Filestream);
byte[] imgdata = oReader.ReadBytes(Convert.ToInt32(Filestream.Length));
byte[] data = new byte[PostArray.Length + imgdata.Length];
myRequest.ContentLength = PostArray.Length + imgdata.Length;
PostArray.CopyTo(data, 0);
imgdata.CopyTo(data, PostArray.Length);
Stream PostStream = myRequest.GetRequestStream();
PostStream.Write(data, 0, data.Length);
PostStream.Close();
获取服务器端的响应
WebResponse webRespon;
try
{
webRespon = myRequest.GetResponse();
}
catch (WebException ex)
{
webRespon = (HttpWebResponse)ex.Response;
}
Stream s = webRespon.GetResponseStream();
StreamReader sr = new StreamReader(s);
strResult = sr.ReadLine();
sr.Close();
s.Close();
Filestream.Close();
return strResult;
} 客户端想服务器POST 参数跟文件 文件发不过去用fidder看参数是带值的 但是到了服务器就收不到了 抛去文件 id 跟code可以传过去 求问大神
分享到:
更多
------解决方案--------------------
你看仔细了,那个就是用的"multipart/form-data"而不是你写的"application/x-www-form-urlencoded",参数传递方式大不相同,你网上查下"multipart/form-data"的写法吧。
例如:http://yefeng.iteye.com/blog/315847