c# post文件参数到服务器

czs06815394 2013-09-09 03:26:03
以下代码传递除文件形式的参数不行,别的类型的参数均是ok的,能够正确拿到服务器返回的信息,
public static string GetResponse(string url, string param, string method)
{
string reStr = "";
if (method.ToUpper() == "GET")
{
url = url + "?" + param;
}
HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(url);
hwrq.Method = method.ToUpper();
hwrq.KeepAlive = true;
if (method.ToUpper() == "POST")
{
ServicePointManager.Expect100Continue = false;
hwrq.ContentType = "application/x-www-form-urlencoded";
byte[] bs = Encoding.UTF8.GetBytes(param);
hwrq.ContentLength = bs.Length;
using (Stream reqStream = hwrq.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
}
hwrq.Timeout = 50000;
using (HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse())
{
while (hwrp.StatusCode != HttpStatusCode.OK)
{
System.Threading.Thread.Sleep(500);
}
Stream st = hwrp.GetResponseStream();
reStr = new StreamReader(st, Encoding.UTF8).ReadToEnd();
st.Close();
st.Dispose();
}
return reStr;
}

那么我现在要传递一个文件类型的参数给服务器(如图片类型),应该怎么实现呢(用上面的犯方法不能正确得到返回数据)???
...全文
290 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2013-10-04
  • 打赏
  • 举报
回复
HttpWebRequest的话,参考这个讨论中的代码:
Upload files with HTTPWebrequest (multipart/form-data)
http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
  • 打赏
  • 举报
回复
传一个文件,使用一句
new WebClient().UploadFile(....)
willhuo 2013-10-03
  • 打赏
  • 举报
回复
正常的提交表单 hwrq.ContentType = "application/x-www-form-urlencoded"; 文件上传因为是二进制的数据上传到服务器,contenttype需要换成multipart
Jxb_Sir 2013-09-10
  • 打赏
  • 举报
回复
http://bbs.yiwowang.com/forum.php?mod=viewthread&tid=596
czs06815394 2013-09-10
  • 打赏
  • 举报
回复
顶啊。。。。。。。。。。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧