【发布时间】:2016-05-02 03:24:36
【问题描述】:
我得到了 ByteArrayContent 的工作
/* MVC Method , ByteArrayContent */
private async Task<HttpResponseMessage> ExecuteProxy(string url)
{
using (var client = new HttpClient(HttpClientHandlerFactory.GetWindowsAuthenticationHttpClientHandler()))
{
byte[] byte1 = new byte[] { 1, 2, 3 };
ByteArrayContent byteContent = new ByteArrayContent(byte1);
this.Request.Method = HttpMethod.Post;
this.Request.Content = byteContent;
return await client.SendAsync(this.Request);
}
}
/* WebApi Delegating Handler , ByteArrayContent */
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var byteArray = request.Content.ReadAsByteArrayAsync().Result;
if (null != byteArray)
{
/* I see the byte of "1,2,3" */
}
}
下面的方法不是问题的一部分,但为了完整起见,我将其包括在内。
public static class HttpClientHandlerFactory
{
public static HttpClientHandler GetWindowsAuthenticationHttpClientHandler()
{
HttpClientHandler returnHandler = new HttpClientHandler()
{
UseDefaultCredentials = true,
PreAuthenticate = true
};
return returnHandler;
}
}
我在“获取”WebApi 方面的 MultipartFormDataContent 时遇到了麻烦。
/* MVC Method , MultipartFormDataContent */
private async Task<HttpResponseMessage> ExecuteProxy(string url)
{
using (var client = new HttpClient(HttpClientHandlerFactory.GetWindowsAuthenticationHttpClientHandler()))
{
byte[] byte1 = new byte[] { 1, 2, 3 };
ByteArrayContent byteContent1 = new ByteArrayContent(byte1);
StringContent stringContent1 = new StringContent("StringContent1Value");
byte[] byte2 = new byte[] { 4, 5, 6 };
ByteArrayContent byteContent2 = new ByteArrayContent(byte2);
StringContent stringContent2 = new StringContent("StringContent2Value");
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(byteContent1, "MyByteArrayContent1");
multipartContent.Add(stringContent1);
multipartContent.Add(byteContent2, "MyByteArrayContent2");
multipartContent.Add(stringContent2);
this.Request.Method = HttpMethod.Post;
this.Request.Content = multipartContent;
return await client.SendAsync(this.Request);
}
}
/* WebApi Delegating Handler , MultipartFormDataContent*/
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
/* I have no idea how to change this back into a MultipartFormDataContent .. or however else you parse it */
}
我在谷歌上搜索并阅读了大约 40 篇关于它的 SOF 帖子。解决方案仍然暗示我。
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-web-api