当我尝试通过fiddler作为标准(unchunked)请求运行完全相同的请求时,它工作正常,但是,我似乎无法读取分块请求.
我尝试了两种不同的方法.第一:
public httpResponseMessage importEstimate(httpRequestMessage request){ var data = request.Content.ReadAsMultipartAsync().Result; IEnumerable<httpContent> parts = data.Contents; return request.CreateResponse(httpStatusCode.OK,parts.Count());}
这不会返回任何东西.它只是坐着,直到请求超时.
第二种方法是:
public httpResponseMessage importEstimate(httpRequestMessage request){ IEnumerable<httpContent> parts = null; Task.Factory .StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents,CancellationToken.None,TaskCreationoptions.LongRunning,// guarantees separate thread TaskScheduler.Default) .Wait(); return request.CreateResponse(httpStatusCode.OK,parts.Count());}
哪个返回错误:
Unexpected end of MIME multipart stream. MIME multipart message is not complete.
我在这里错过了什么?
编辑:以下是WireShark的请求
POST /myservice/importestimate http/1.1Host: devAPI.mydomain.comContent-Type: multipart/related; type="application/xop+xml"; start="<start.xml>"; start-info="text/xml"; boundary="--MIME_boundary"transfer-encoding: chunkedSOAPAction: "importestimate"X-Forwarded-For: xxx.xx.xxx.xxxConnection: close94----MIME_boundaryContent-Type: application/xop+xml; type="text/xml; charset=UTF-8"Content-transfer-encoding: 8bitContent-ID: <start.xml>170<?xml version='1.0' enCoding='UTF-8' ?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xop="http://www.w3.org/2004/08/xop/include" ><soap:Body><XDOC><XNET_INFO transactionID="001P92V" ><ATTACHMENTS><ATTACHMENT><xop:Include href="cID:3203@xactware.com" /></ATTACHMENT></ATTACHMENTS></XNET_INFO></XDOC></soap:Body></soap:Envelope>B3----MIME_boundaryContent-Type: application/zipContent-transfer-encoding: binaryContent-disposition: attachment; filename="XDOC.ZIP"Content-ID: <3203@x.com>5BC... lots of data here removed for brevity...15----MIME_boundary--0解决方法 经过大量研究,我发现了问题!
显然我的请求并没有以CRLF结束,.Net必须要求发出请求结束的信号.
我最终阅读了整个请求,添加了一个CRLF,并从MemoryStream创建了自己的ReadAsMultipartAsync.这似乎有效.
编辑添加代码:
private byte[] Processinput(httpRequestMessage request){ List<httpContent> parts = new List<httpContent>(); byte[] result; result = request.Content.ReadAsByteArrayAsync().Result; // StupID chunked requests remove the final CRLF,which makes .Net puke on the request. // So add our own CRLF. List<byte> crlfTemp = result.ToList(); crlfTemp.Add(0x0D); crlfTemp.Add(0x0A); result = crlfTemp.ToArray(); // Convert stream to MIME using (var stream = new MemoryStream(result)) { // note: StreamContent has no Content-Type set by default // set a suitable Content-Type for ReadAsMultipartAsync() var content = new StreamContent(stream); content.headers.ContentType = System.Net.http.headers.MediaTypeheaderValue.Parse( "multipart/related; boundary=--MIME_boundary"); content.headers.ContentLength = result.Length; bool isMPC = content.IsMimeMultipartContent(); Task.Factory .StartNew(() => parts = content.ReadAsMultipartAsync().Result.Contents.ToList(),// guarantees separate thread TaskScheduler.Default) .Wait(); }}总结
以上是内存溢出为你收集整理的c# – WebApi无法读取分块请求全部内容,希望文章能够帮你解决c# – WebApi无法读取分块请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)