如何在C#中用Http的Post请求发送一个图片?

xieyueqing 2008-09-17 11:19:36
例如:
地址:www.test.com/test.php
参数:id 数字
file 图片文件内容

现在可以取得包含图片内容的stream对象,如何把它发出去?谢谢!
...全文
1349 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhejianwei123 2011-12-09
  • 打赏
  • 举报
回复
sunbluebird 2008-09-17
  • 打赏
  • 举报
回复
不知群里有没有人懂,呵呵..

C#技术交流群:55180062 一起学习,一起成长
liuyilidan 2008-09-17
  • 打赏
  • 举报
回复
这里只是把数据传送到www.test.com/test.php上去 具体的操作还是在www.test.com/test.php上 比如保存等
c#服务器端代码为:
Stream oStream = Request.InputStream;
if (oStream.Length == 0)
{
return;
}
else
{
string strPath = "文件保存路径+要保存的文件的名称";
FileInfo oFile = new FileInfo(strPath); //获取服务器端文件
if(!oFile.Exists) //不存在相同文件则保存
{
Request.SaveAs(strPath, false);
}
}
oStream.Close();
xieyueqing 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liuyilidan 的回复:]
利用Webclient将本地图片上传到服务器指定地址
string strRequest = "www.test.com/test.php";
string strFile = "本地图片路径";
WebClient oWebClient = new WebClient();
FileStream oStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
BinaryReader oReader = new BinaryReader(oStream);
Byte[] postArray = oReader.ReadBytes(Convert.ToInt32(oStream.Length));
Stream postStream = oWe…
[/Quote]
多谢!不过怎么把 参数名字写进去呢?还有其他字符型参数也写进去。
liuyilidan 2008-09-17
  • 打赏
  • 举报
回复
利用Webclient将本地图片上传到服务器指定地址
string strRequest = "www.test.com/test.php";
string strFile = "本地图片路径";
WebClient oWebClient = new WebClient();
FileStream oStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
BinaryReader oReader = new BinaryReader(oStream);
Byte[] postArray = oReader.ReadBytes(Convert.ToInt32(oStream.Length));
Stream postStream = oWebClient.OpenWrite(strRequest, "POST");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();
billlyh 2008-09-17
  • 打赏
  • 举报
回复
ding!!!!!!!!!!
xieyueqing 2008-09-17
  • 打赏
  • 举报
回复
顶起来!
xieyueqing 2008-09-17
  • 打赏
  • 举报
回复

Stream oStream;
string postData;
HttpWebRequest myRequest;
BinaryReader oReader;

string imgWebsite = @"http://192.168.169.11/service/deal_image_service.php";

postData = "command=UPLOAD"; //参数1
postData += "&id="; //参数2
postData += "&image_name=imgtest1" ; //参数3
postData += "&image_type=0" ; //参数4
postData += "&poi_id=111" ; //参数5
postData += "&temp_id=3"; //参数6
postData += "&file="; //参数7,这个后面跟的就是图片内容了
byte[] pardata = Encoding.Default.GetBytes(postData);
//oStream = img.GetFileContent();
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[] data = new byte[postData.Length + imgdata.Length];
//把前面的字符串和图片的byte数组连起来,这样做对么????
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();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string findokID = reader.ReadToEnd();

我写了一个这样的代码,发送过去后对方返回一个失败,也不知道是我的问题,还是对方的php有问题。
xieyueqing 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 liuyilidan 的回复:]
多个文件还没做过 以前也找过 好象没什么好的解决方案
[/Quote]

一个文件+几个参数
woaixueyu 2008-09-17
  • 打赏
  • 举报
回复
URL上加撒 url?xx=wef
liuyilidan 2008-09-17
  • 打赏
  • 举报
回复
多个文件还没做过 以前也找过 好象没什么好的解决方案
xieyueqing 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liuyilidan 的回复:]
这里只是把数据传送到www.test.com/test.php上去 具体的操作还是在www.test.com/test.php上 比如保存等
c#服务器端代码为:
Stream oStream = Request.InputStream;
if (oStream.Length == 0)
{
return;
}
else
{
string strPath = "文件保存路径+要保存的文件的名称";
FileInfo oFile = new FileInfo(strPath); //获取服务器端文件
if(!oFile.Exists) //不存在相…
[/Quote]

不是这个意思,我是要一次post把多个参数传过去,具体那边怎么接受我就不管了。

我是不知道如何同时传好几个参数,Get方法我知道,Post我就不知道了。

110,536

社区成员

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

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

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