简体   繁体   中英

shrinking the data I send c# rest api post

I have a one question for yours. I have dev. one project, I need your helps. I have one post service to be taken byte array and will be convert to image again for me. I have one picture, i converted this image to byte[], I did send byte[] using BinaryWriter if you wants looking my code

        public void SendData()
    {
        var item = Directory.GetFiles(@"C:\Users\nazmi\Desktop\s");
        //Bitmap bmp = new Bitmap(item[0]);
        //var stream = new MemoryStream();
        //bmp.Save(stream, ImageFormat.Jpeg);
        //var imageBytes = stream.ToArray();
        Bitmap bmp = null;
        bmp = new Bitmap(item[0]);
        var stream = new MemoryStream();
        bmp.Save(stream, bmp.RawFormat);
        var imageBytes = stream.ToArray();
       // string sendString = System.Text.Encoding.UTF8.GetString(imageBytes);
        //MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
        //ms.Write(imageBytes, 0, imageBytes.Length);
        //Image returnImage = Image.FromStream(ms, true);
        //returnImage.Save(@"C:\Users\Nazmi\Desktop\123.tiff");
        // File.WriteAllBytes(@"C:\Users\Nazmi\Desktop\123.txt", imageBytes);
        var url = "http://localhost:28862/api/BinaryEncodeDecode";
        var httpRequest = (HttpWebRequest)WebRequest.Create(url);
        httpRequest.Method = "POST";
        httpRequest.Accept = "application/json";
        httpRequest.ContentType = "application/json";
        var data = @"{ 'byteArray': '" + imageBytes + "'  }";
        using (var writer = new BinaryWriter(httpRequest.GetRequestStream()))
        {
            
            writer.Write(data);
            var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
        }

        //using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
        //{
        //    streamWriter.Write(data);
        //}

      
    }

And will be share with you my rest service you are looking my code if you want ->

  public async Task<IActionResult> PostAsync()
    {
      
        try
        {
            var binary =  new BinaryReader(Request.Body);
            byte[] allData = binary.ReadBytes(1054);
            return Ok("Başarılı");
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
            throw;
        }


    }

My byte[] length is so big but i taken only 34. I tried so many way. Please help me.

application/json is for communicating with JSON encoding, byte array is not a json object. you need to set your httpRequest.Accept and httpRequest.ContentType to application/octet-stream

this link can help you

i solved this problem.

         int length = (int)(Request.ContentLength ?? 0);
            byte[] content = new byte[length];
           await Request.Body.ReadAsync(content, 0, length);

this code block solved my problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM