我有一个Microsoft ASP.net Web API项目(使用.net Framework 4.61),该项目具有API方法,应该接受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标题生成以下结果:
multipart/related;type=application/dicom+xml;boundary=MESSAGEBOUNDARY
- >触发异常multipart/related;type="application/dicom+xml";boundary=MESSAGEBOUNDARY
- >输出确定multipart/related;type=application-dicom+xml;boundary=MESSAGEBOUNDARY
- >输出确定似乎.net中的多部件处理无法处理Content-Type标头的type
参数中出现的斜杠,除非该值嵌入双序列中,尽管就我而言理解RFC,在这种情况下使用双引号是可选的。
作为参考,这是一些简单的代码,您可以使用其他应用程序发送帖子请求:
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 = @"--MESSAGEBOUNDARY
Content-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");
}
答案 0 :(得分:0)
再次阅读RFC2045第5.1章(内容类型标题字段的语法),我发现斜杠字符实际上确实使用了双引号强制:
tspecials := "(" / ")" / "<" / ">" / "@" /
"," / ";" / ":" / "\" / <">
"/" / "[" / "]" / "?" / "="
; Must be in quoted-string,
; to use within parameter values
因此,微软的实施是正确的。