简体   繁体   English

HttpResponseMessage 内容未返回正确的内容类型

[英]HttpResponseMessage Content not returning the correct content type

I am trying to return an Xml File and a Class as JSON from my web api as part of business requirements as they do not like to send XML within JSON. I am trying to return an Xml File and a Class as JSON from my web api as part of business requirements as they do not like to send XML within JSON.

But the content type of the response is not "multipart/form-data"但响应的内容类型不是“multipart/form-data”

API Code C#.Net Core API 代码 C#.Net Core

        [HttpPost("GetMessage")]
    public HttpResponseMessage GetMessage([FromForm]BaseInput baseInput)
    {
        MultipartContent MultiContent = new MultipartContent("form-data");
        HttpResponseMessage ResponseMessage = new HttpResponseMessage();

        StringContent contentMessageJSON = CreateJSONContent(JsonConvert.SerializeObject(rtnVal));
        StringContent contentMessagexml = CreateBusinessMessageContent(rtnVal.MessageXML, "BUSINESSMESSAGE.XML");

        MultiContent.Add(contentMessagexml);
        MultiContent.Add(contentMessageJSON);
        ResponseMessage.Content = MultiContent;

        return ResponseMessage;
    }


public StringContent CreateJSONContent(string strJSON)
    {
        StringContent contentData;
        contentData = new StringContent(strJSON);
        try
        {
            contentData.Headers.Remove("Content-Type");
            contentData.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "binary");
            contentData.Headers.TryAddWithoutValidation("Content-ID", Guid.NewGuid().ToString());
            contentData.Headers.TryAddWithoutValidation("Content-Disposition", "form-data;name=METADATA");
        }
        catch (Exception ex)
        {
        }
        return contentData;
    }

    public StringContent CreateBusinessMessageContent(string businessmessage, string filename)
    {
        StringContent contentMessagexml = new StringContent(businessmessage);
        try
        {
            contentMessagexml.Headers.Remove("Content-Type");
            contentMessagexml.Headers.TryAddWithoutValidation("Content-Disposition", string.Format("form-data; name=BUSINESSMESSAGE; filename={0}", filename));
            contentMessagexml.Headers.TryAddWithoutValidation("Content-Type", "text/xml");
        }
        catch (Exception ex)
        {
        }
        return contentMessagexml;
    }

Client Code VB Winforms客户端代码 VB Winforms

        Dim httpresponse As HttpResponseMessage

        'Now send it
        Try

            httpresponse = Await httpClient.PostAsync(txtEndPoint.Text, content)
            Dim MultiPartStreamProv As MultipartStreamProvider = Await httpresponse.Content.ReadAsMultipartAsync()

            Dim stringContent = Await MultiPartStreamProv.Contents(0).ReadAsStringAsync()
            Dim streamContent = Await MultiPartStreamProv.Contents(1).ReadAsStreamAsync()
        Catch ex As Exception

        End Try

In The Client Code, i get the error "Invalid 'HttpContent' instance provided. It does not have a content type header starting with 'multipart/'. Parameter name: content"在客户端代码中,我收到错误“提供的‘HttpContent’实例无效。它没有以‘multipart/’开头的内容类型 header。参数名称:内容”

i Checked and the content type is Application/json, no idea why.我检查了,内容类型是 Application/json,不知道为什么。

I am trying to return an Xml File and a Class as JSON from my web api as part of business requirements as they do not like to send XML within JSON. I am trying to return an Xml File and a Class as JSON from my web api as part of business requirements as they do not like to send XML within JSON.

But the content type of the response is not "multipart/form-data"但响应的内容类型不是“multipart/form-data”

API Code C#.Net Core API 代码 C#.Net Core

        [HttpPost("GetMessage")]
    public HttpResponseMessage GetMessage([FromForm]BaseInput baseInput)
    {
        MultipartContent MultiContent = new MultipartContent("form-data");
        HttpResponseMessage ResponseMessage = new HttpResponseMessage();

        StringContent contentMessageJSON = CreateJSONContent(JsonConvert.SerializeObject(rtnVal));
        StringContent contentMessagexml = CreateBusinessMessageContent(rtnVal.MessageXML, "BUSINESSMESSAGE.XML");

        MultiContent.Add(contentMessagexml);
        MultiContent.Add(contentMessageJSON);
        ResponseMessage.Content = MultiContent;

        return ResponseMessage;
    }


public StringContent CreateJSONContent(string strJSON)
    {
        StringContent contentData;
        contentData = new StringContent(strJSON);
        try
        {
            contentData.Headers.Remove("Content-Type");
            contentData.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "binary");
            contentData.Headers.TryAddWithoutValidation("Content-ID", Guid.NewGuid().ToString());
            contentData.Headers.TryAddWithoutValidation("Content-Disposition", "form-data;name=METADATA");
        }
        catch (Exception ex)
        {
        }
        return contentData;
    }

    public StringContent CreateBusinessMessageContent(string businessmessage, string filename)
    {
        StringContent contentMessagexml = new StringContent(businessmessage);
        try
        {
            contentMessagexml.Headers.Remove("Content-Type");
            contentMessagexml.Headers.TryAddWithoutValidation("Content-Disposition", string.Format("form-data; name=BUSINESSMESSAGE; filename={0}", filename));
            contentMessagexml.Headers.TryAddWithoutValidation("Content-Type", "text/xml");
        }
        catch (Exception ex)
        {
        }
        return contentMessagexml;
    }

Client Code VB Winforms客户端代码 VB Winforms

        Dim httpresponse As HttpResponseMessage

        'Now send it
        Try

            httpresponse = Await httpClient.PostAsync(txtEndPoint.Text, content)
            Dim MultiPartStreamProv As MultipartStreamProvider = Await httpresponse.Content.ReadAsMultipartAsync()

            Dim stringContent = Await MultiPartStreamProv.Contents(0).ReadAsStringAsync()
            Dim streamContent = Await MultiPartStreamProv.Contents(1).ReadAsStreamAsync()
        Catch ex As Exception

        End Try

In The Client Code, i get the error "Invalid 'HttpContent' instance provided. It does not have a content type header starting with 'multipart/'. Parameter name: content"在客户端代码中,我收到错误“提供的‘HttpContent’实例无效。它没有以‘multipart/’开头的内容类型 header。参数名称:内容”

i Checked and the content type is Application/json, no idea why.我检查了,内容类型是 Application/json,不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM