HttpWebRequest 上传图片

T_long 2011-05-04 03:58:58
...全文
603 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxldfang 2011-05-05
  • 打赏
  • 举报
回复
你需要将所有的表单字段及值按要求(参你的另一贴)生成字节数组并将数据post到处理程序
CShareMySide 2011-05-05
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 t_long 的回复:]
TMD 我把发送总信息的地址当成发送图片的地址了
扯淡,太扯蛋了……
[/Quote]

能找到问题就好
T_long 2011-05-05
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sxldfang 的回复:]
你这是往哪里上传图片啊,网址是多少,我试试看!
[/Quote]
马上就成功了,呵呵,谢谢了!!!
T_long 2011-05-05
  • 打赏
  • 举报
回复
TMD 我把发送总信息的地址当成发送图片的地址了
扯淡,太扯蛋了……
sxldfang 2011-05-05
  • 打赏
  • 举报
回复
你这是往哪里上传图片啊,网址是多少,我试试看!
kingdom_0 2011-05-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 t_long 的回复:]

引用 6 楼 effun 的回复:
用WebClient吧,很多东西都已经包装好了,直接用就是了。

整个项目都用的webrequest,改的话太麻烦了……
[/Quote]
在coding之前做好分析很重要。
T_long 2011-05-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jeogegxs 的回复:]
C# code

post.AppendFormat("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);


最前面少了\r\n
改为

C# code

post.AppendFormat("\r\n--{0}\r\nConte……
[/Quote]
是在循环外面加post.Append("\r\n");吧
加里面了格式就变了,后面有个{2}\r\n
可是加上了也不行,再帮忙看看有什么毛病……
jeogegxs 2011-05-05
  • 打赏
  • 举报
回复

post.AppendFormat("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);

最前面少了\r\n
改为

post.AppendFormat("\r\n--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);
T_long 2011-05-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 effun 的回复:]
用WebClient吧,很多东西都已经包装好了,直接用就是了。
[/Quote]
整个项目都用的webrequest,改的话太麻烦了……
effun 2011-05-05
  • 打赏
  • 举报
回复
用WebClient吧,很多东西都已经包装好了,直接用就是了。
T_long 2011-05-05
  • 打赏
  • 举报
回复
我想知道我写的那个格式有什么不对,我那个怎么上传不上去?
不加参数就上传成功,加上参数就不行了……
T_long 2011-05-04
  • 打赏
  • 举报
回复
这是我的代码……
String contenttype = "image/pjpeg";
//string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
try
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(fileToUpload));
//sb.Append(fileToUpload);
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");

//string postHeader = sb.ToString() + postDateAdd;
string postData = "";
if (postDateAdd != "")
{
StringBuilder post = new StringBuilder();
string[] array = postDateAdd.Split('&');

foreach (string str in array)
{
string[] arr = str.Split('=');
post.AppendFormat("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);
}
post.AppendFormat("--{0}--\r\n", boundary);
postData = post.ToString();
}

string postHeader = sb.ToString();

byte[] postHeaderBytes = encoding.GetBytes(postHeader);

byte[] postDateAddBytes = encoding.GetBytes(postData);

//byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);

//long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length + postDateAddBytes.Length;

long length = postHeaderBytes.Length + fileStream.Length + postDateAddBytes.Length;
webrequest.ContentLength = length;
webrequest.CookieContainer = cc;
webrequest.AllowAutoRedirect = true;

webrequest.ServicePoint.ConnectionLimit = maxTry;
webrequest.Referer = uploadUrl;
webrequest.Accept = accept;
webrequest.UserAgent = userAgent;

Stream requestStream = webrequest.GetRequestStream();

requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

byte[] buffer = new Byte[(int)fileStream.Length];

int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);

//requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

requestStream.Write(postDateAddBytes, 0, postDateAddBytes.Length);

HttpWebResponse res = (HttpWebResponse)webrequest.GetResponse();

StreamReader sr = new StreamReader(res.GetResponseStream(), encoding);
string html = sr.ReadToEnd();


requestStream.Close();
return html;
xuexiaodong2009 2011-05-04
  • 打赏
  • 举报
回复
根据HttpWebRequest 获取一个流,再跟流生成file.然后写入文件

110,534

社区成员

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

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

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