我正在尝试使用HttpClient将WPF捕获的图像发布到WebApi方法,但出现400错误请求错误。 我在Google中尝试过,但无法解决问题。有人帮我吗?
下面是WPF中的代码
private async void btnLogin_Click(object sender, RoutedEventArgs e)
{
string FileName =
System.IO.Path.GetFullPath("../../captured_images") +
"//captured_image" + DateTime.Now.Day.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() +
DateTime.Now.Second.ToString() + ".jpg";
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(FileName,
FileMode.Create))
encoder.Save(stream);
string CASAAuthResponse = await
CASSecurity.GetAuthenticationToken();
CASAuthTokenResponse techSeeTokenResponse =
JsonConvert.DeserializeObject<CASAuthTokenResponse>
(CASAAuthResponse);
HttpContent fileStreamContent = new StreamContent(File.OpenRead(FileName));
using (var client1 = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
client1.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
formData.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
formData.Add(fileStreamContent, "face",
Path.GetFileName(FileName));
var response = await
client1.PostAsync(CASIdentifyFaceUrl, formData);
if (!response.IsSuccessStatusCode)
{
return null;
}
}
}
服务器Web API:
[HttpPost]
[Route("identify")]
public async Task<IActionResult> Identify(IFormFile face)
{
Guid temporaryUsername = Guid.Empty;
using (var faceStream = face.OpenReadStream())
{
temporaryUsername = await verifyBusiness.IdentifyUser(faceStream,
new Guid(Requester.ClientId));
}
return Ok(temporaryUsername);
}
我收到以下错误消息:{StatusCode:400, ReasonPhrase:“错误请求”,版本:1.1,内容: System.Net.Http.StreamContent,标头:{传输编码:分块 严格运输安全:max-age = 2592000日期:2019年6月20日,星期四 11:13:28 GMT Set-Cookie: ARRAffinity = 4cbc3e777eee0146fcbb9f695794b29417cc953731f6f8f581457a1d7cd7aa14; Path = /; HttpOnly; Domain = cas-qa.tempdata.net 服务器:Kestrel X技术支持:ASP.NET内容类型: application / json; charset = utf-8}}