I got the ByteArrayContent to work
/* 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" */
}
}
The below method isn't part of the problem, but I include it for completeness.
public static class HttpClientHandlerFactory
{
public static HttpClientHandler GetWindowsAuthenticationHttpClientHandler()
{
HttpClientHandler returnHandler = new HttpClientHandler()
{
UseDefaultCredentials = true,
PreAuthenticate = true
};
return returnHandler;
}
}
I'm having trouble "getting" the MultipartFormDataContent on the WebApi side of things.
/* 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 */
}
I've googled and read about 40 SOF posts about it. The solution still alludes me.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…