Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to post captured image from WPF to WebApi method using HttpClient but i am getting 400 BAD REQUEST error.
I have tried in google but unable to resolve the issue. does anyone help me.
Below is the code in WPF
C#
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;
                        }
                        
                    }

            

        }

Server Web api:


C#
[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);
    }

> And i am getting error as descibed below:

{StatusCode: 400,
> ReasonPhrase: 'Bad Request', Version: 1.1, Content:
> System.Net.Http.StreamContent, Headers: { Transfer-Encoding: chunked
> Strict-Transport-Security: max-age=2592000 Date: Thu, 20 Jun 2019
> 11:13:28 GMT Set-Cookie:
> ARRAffinity=4cbc3e777eee0146fcbb9f695794b29417cc953731f6f8f581457a1d7cd7aa14;Path=/;HttpOnly;Domain=cas-qa.tempdata.net
> Server: Kestrel X-Powered-By: ASP.NET Content-Type:
> application/json; charset=utf-8 }}

What I have tried:

I HAVE TRIED DIFERRENT TRIAL AND ERRORS BUT NONO WORKING
Posted
Updated 2-Aug-19 3:23am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900