post 文件时报错 Stream ended unexpectedly

荷塘夜色 2010-02-05 11:35:30
string keys = "name,pwd,msisdn,subject,linkid";
string[] key = keys.Split(',');
string values = "XXXX,XXXX,159XXXXX67,XXXXX,0";
string[] value = values.Split(',');
string fileNames = "cx/post/1.jpg ,cx/post/2.jpg ,cx/post/3.jpg ,cx/post/4.jpg ,cx/post/1.txt ,cx/post/2.txt ,cx/post/3.txt ,cx/post/5.smil";
string[] fileName = fileNames.Split(',');
Response.Write(SubmitData(fileName, "http://www.xxxx.com/sssss", key, value));

public string SubmitData(string[] fileName, string uri, string[] keys, string[] values)
{
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Timeout = 200000;
httpWebRequest2.Method = "POST";
httpWebRequest2.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";


StringBuilder sb = new StringBuilder();

if (keys != null)
{
for (int i = 0; i < keys.Length; i++)
{
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + keys[i] + "\"\r\n\r\n");
sb.Append(values[i]);
sb.Append("\r\n");
}
}
if (fileName != null)
{
for (int In = 0; In < fileName.Length; In++)
{
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"file" + In + "\"; filename=\"");
sb.Append(Path.GetFileName(System.Web.HttpContext.Current.Server.MapPath(fileName[In])));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
}
}
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
long lengthFile = 0;
if (fileName != null)
{
for (int In = 0; In < fileName.Length; In++)
{
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath(fileName[In]), FileMode.Open, FileAccess.Read);
lengthFile += fileStream.Length;
}
}
long length = postHeaderBytes.Length + lengthFile + boundaryBytes.Length;
httpWebRequest2.ContentLength = length;

Stream requestStream = httpWebRequest2.GetRequestStream();

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

byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)lengthFile))];
int bytesRead = 0;
if (fileName != null)
{
for (int In = 0; In < fileName.Length; In++)
{
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath(fileName[In]), FileMode.Open, FileAccess.Read);
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
}
}
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
try
{
HttpWebResponse rep = (HttpWebResponse)httpWebRequest2.GetResponse();
Stream receiveStream = rep.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader readStream = new StreamReader(receiveStream, encode);

Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb1 = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb1.Append(readstr);
count = readStream.Read(read, 0, 256);
}

rep.Close();
readStream.Close();
string Xhtml = sb1.ToString();
return Xhtml;
}
catch (WebException ex)
{
return ex.Message.ToString();
}
}
...全文
1724 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
荷塘夜色 2010-02-05
  • 打赏
  • 举报
回复
哪位大哥帮忙解决下啊

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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