[英]How to return multipart/form-data from WCF Service in response
I want my WCF Service to return multipart/form-data.我希望我的 WCF 服务返回多部分/表单数据。 Although the response body does contain the required content, the response headers indicate that the body is application/octet-stream when I want to see Content-type: multipart/form-data.
尽管响应正文确实包含所需的内容,但是当我想查看 Content-type: multipart/form-data 时,响应标头表明正文是 application/octet-stream。
The code begins with the following unit test.代码从以下单元测试开始。
[TestMethod, Owner(TEST_AUTHOR), TestCategory("MIME Testing")]
public void TempWcfService_PostMimeRequest_ExpectSuccess()
{
//Arrange
HttpClient client = new HttpClient();
HttpMultipartMimeForm form = new HttpMultipartMimeForm();
form.Add("Key1", "Value1");
form.Add("Key2", "Value2");
//Act
HttpResponseMessage response = client.Post(TEST_HOST_URL + "/mime-test", form.CreateHttpContent());
response.EnsureStatusIsSuccessful();
Stream responseStream = response.Content.ReadAsStream();
string sResponse = responseStream.ToString();
//Assert
Console.Write(sResponse);
}
The above test consumes a WCF Service whose interface looks like the following.上面的测试使用了一个 WCF 服务,它的界面如下所示。
namespace TempWcfService
{
/// <summary>
/// Interface added to support http without the WCF client class
/// </summary>
[ServiceContract]
public interface ITempPing
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "ping")]
[Description("Pings the service for availability")]
string ping();
[OperationContract]
[WebInvoke(UriTemplate = "mime-test",
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare)]
[Description("Tests a simple MIME request")]
Stream TestMultipartMime(Stream data);
}
}
And the implementation...和实施...
namespace TempWcfService
{
/// <summary>
/// Implementation added to support http without the WCF client class
/// </summary>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class TempPing : ITempPing
{
public string ping()
{
return "TempPing ping() successful";
}
public Stream TestMultipartMime(Stream data)
{
// Re-construct the multipart/form-data content
MultipartFormData multipartFormData = new MultipartFormData(WebOperationContext.Current.IncomingRequest, data);
HttpMultipartMimeForm responseForm = new HttpMultipartMimeForm(multipartFormData.Items);
responseForm.Add("ErrorCode", "Success");
responseForm.Add("ErrorMessage", "None");
HttpResponseMessage responseMsg = new HttpResponseMessage();
responseMsg.StatusCode = System.Net.HttpStatusCode.OK;
responseMsg.Content = responseForm.CreateHttpContent();
responseMsg.Headers.Add("Accept", "multipart/form-data");
responseMsg.Headers.ContentType = "multipart/form-data; boundary=" + multipartFormData.Boundary;
return responseMsg.Content.ReadAsStream();
}
}
}
WCF seems to be 'helping me' by overriding the ContentType specified in the code with a Content Type of 'application/octet-stream' based on the content returned using ReadAsStream(). WCF 似乎通过基于使用 ReadAsStream() 返回的内容使用“应用程序/八位字节流”的内容类型覆盖代码中指定的 ContentType 来“帮助我”。 Is there a way to get the Content-Type header to indicate multipart/form-data?
有没有办法让 Content-Type 标头指示多部分/表单数据?
You'll need to set the content type in the outgoing context:您需要在传出上下文中设置内容类型:
public Stream TestMultipartMime(Stream data)
{
// Re-construct the multipart/form-data content
MultipartFormData multipartFormData = new MultipartFormData(WebOperationContext.Current.IncomingRequest, data);
HttpMultipartMimeForm responseForm = new HttpMultipartMimeForm(multipartFormData.Items);
responseForm.Add("ErrorCode", "Success");
responseForm.Add("ErrorMessage", "None");
HttpResponseMessage responseMsg = new HttpResponseMessage();
responseMsg.StatusCode = System.Net.HttpStatusCode.OK;
responseMsg.Content = responseForm.CreateHttpContent();
WebOperationContext.Current.OutgoingResponse.ContentType = "multipart/form-data; boundary=" + multipartFormData.Boundary;
WebOperationContext.Current.OutgoingResponse.Headers["Accept"] = "multipart/form-data";
return responseMsg.Content.ReadAsStream();
}
Notice that you shouldn't need to set the Accept
header in the response - this header is used in requests for clients to tell the server which content-types it wants to receive.请注意,您不需要在响应中设置
Accept
标头 - 此标头用于客户端请求以告诉服务器它想要接收哪些内容类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.