.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方法如下:
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();
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
48555
----------053112154924859
Content-Disposition: form-data; name="filePath"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
/1/2012/5/767/767_O.docx
----------053112154924859
Content-Disposition: form-data; name="files"; filename="767_O.docx.jxup"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
PK
\\以后是文件包,省略。。。。
有问题的.net抓包结果:
POST /journalx_zwxb/WordPlus/upload.action HTTP/1.0
Accept-Encoding: identity
Content-Type: multipart/form-data; boundary=----------634740777461875000
Accept: text/html, */*
User-Agent: Mozilla/3.0 (compatible; Indy Library)
Host: 172.19.41.36:8080
Content-Length: 33768
Connection: Keep-Alive
----------634740777461875000
Content-Disposition: form-data; name="safeCode"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
48555
----------634740777461875000
Content-Disposition: form-data; name="filePath"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
/1/2012/5/767/767_O.docx
----------634740777461875000
Content-Disposition: form-data; name="files";filename="767_O.docx.jxup"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
PK
\\以后是文件包,省略。。。。
[解决办法]
你给我发私信,留下邮箱。
我发给你。代码是从晚上淘来的,改了几个bug。
现在比较稳定了。