我有一個Microsoft ASP.net Web API項目(使用.net Framework 4.61),該項目具有API方法,這些方法應(yīng)該接受POST請求,其中Post包含MIME多部分消息。.net Framework具有這些方法,HttpContentMultipartExtensions.IsMimeMultipartContent并且HttpContentMultipartExtensions.ReadAsMultipartAsync能夠自動處理MIME多部分消息。在WebAPI控制器中使用以下示例代碼:public class MySampleController : ApiController{ public IHttpActionResult Post() { if(this.Request.Content.IsMimeMultipartContent()) { return Json("OK"); } else { throw new Exception("No Multipart"); } }}對于Post請求中給定的Content-Type標(biāo)頭,這將產(chǎn)生以下結(jié)果:multipart/related;type=application/dicom+xml;boundary=MESSAGEBOUNDARY ->觸發(fā)異常multipart/related;type="application/dicom+xml";boundary=MESSAGEBOUNDARY ->輸出確定multipart/related;type=application-dicom+xml;boundary=MESSAGEBOUNDARY ->輸出確定似乎.net中的Multi Part處理無法處理typeContent-Type標(biāo)頭的參數(shù)中出現(xiàn)的斜線,除非該值嵌入在雙引號中,盡管據(jù)我所知,RFC使用雙引號在這種情況下是可選的。WebAPI項目或IIS中是否有一些設(shè)置可以解決此問題?有沒有一種方法可以通過代碼解決此問題?還是行為標(biāo)準(zhǔn)符合要求并且雙引號是必需的?作為參考,這是一些簡單的代碼,您可以從另一個應(yīng)用程序使用該代碼來發(fā)送發(fā)布請求:private void buttonSend_Click(object sender, EventArgs e){ HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost/projectname/api/mysample"); myRequest.Method = "POST"; // Server call fails when the double quotes around the type value are removed myRequest.ContentType = "multipart/related;type=\"application/dicom+xml\";boundary=MESSAGEBOUNDARY"; string body = @"--MESSAGEBOUNDARYContent-Type: application/dicom+xml<?xml version=""1.0"" encoding=""UTF-8""?><NativeDicomModel></NativeDicomModel>--MESSAGEBOUNDARY--"; var data = Encoding.Default.GetBytes(body); myRequest.ContentLength = data.Length; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); var lResponse = myRequest.GetResponse(); MessageBox.Show("OK");}
- 1 回答
- 0 關(guān)注
- 585 瀏覽
添加回答
舉報
0/150
提交
取消