日期:2014-05-18  浏览次数:20970 次

.net使用HttpWebRequest POST文件问题
.net使用HttpWebRequest POST文件问题

服务器端使用java开发,有一个表单提交页面
客户端以前有一个用delphi开发,使用Tidhttp+TIdMultiPartFormDataStream的方式可以正常上传。
现用.net开发和以前一样的上传功能出现问题。

使用.net做POST时发现服务器似乎是无法接收到我传的参数,如果直接把参数加到要POST的页面链接上则可以接收到,但文件依然无法传上去。
java端使用tomcat做服务器,使用.net POST的时候会抛一个Processing of multipart/form-data request failed. Stream ended unexpectedly异常

经抓包对比,使用原有Delphi写的上传程序和.Net写的发的包除个别参数顺序不一样以外,其他几乎完全一致。

这个问题困扰了我很长时间,也换了各种方法尝试依然不能正确上传,请大家帮忙看看,十分感谢!

.net做POST方法如下:
C# code

string boundary = "--------" + DateTime.Now.Ticks.ToString();
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.CookieContainer = cookies;
webrequest.KeepAlive = true;
webrequest.ProtocolVersion = HttpVersion.Version10;
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
webrequest.Accept = "text/html, */*";
webrequest.UserAgent = "Mozilla/3.0 (compatible; Indy Library)";

StringBuilder sb = new StringBuilder();
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"safeCode\"");\\此参数无法接收到
sb.Append("\r\n");
sb.Append("Content-Type: text/plain");
sb.Append("\r\n");
sb.Append("Content-Transfer-Encoding: quoted-printable");
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("48555");
sb.Append("\r\n");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"filePath\"");\\此参数无法接收到
sb.Append("\r\n");
sb.Append("Content-Type: text/plain");
sb.Append("\r\n");
sb.Append("Content-Transfer-Encoding: quoted-printable");
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("/1/2012/5/767/767_O.docx");
sb.Append("\r\n");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"files\";");\\此参数无法接收到
sb.Append("filename=\"");
sb.Append("767_O.docx.jxup");
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("Content-Transfer-Encoding: binary");
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");

FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

byte[] buffer = new Byte[checked((uint)Math.Min(32768, (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);

webrequest.Timeout = 1000000;

WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s);
string str = sr.ReadToEnd();

fileStream.Close();
requestStream.Close();
sr.Close();
s.Close();
responce.Close();



抓包结果:

正常的Delphi抓包结果:
POST /WordPlus/upload.action HTTP/1.0
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------053112154924859
Content-Length: 35265
Host: 172.19.41.36:8080
Accept: text/html, */*
Accept-Encoding: iden