【问题标题】:shrinking the data I send c# rest api post缩小我发送的数据 c#rest api post
【发布时间】:2022-08-19 20:22:28
【问题描述】:

我有一个问题要问你。我有开发。一个项目,我需要你的帮助。我有一项邮政服务要采用字节数组,并将再次为我转换为图像。我有一张图片,我将此图像转换为字节 [],如果您想查看我的代码,我确实使用 BinaryWriter 发送了字节 []

        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);
        //}

      
    }

并将与您分享我的休息服务,如果您愿意,您正在查看我的代码 ->

  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;
        }


    }

我的 byte[] 长度很大,但我只取了 34 。 我尝试了很多方法。请帮我 。

    标签: c# image bitmap


    【解决方案1】:

    application/json 用于与 JSON 编码通信,字节数组不是 json 对象。您需要将 httpRequest.AccepthttpRequest.ContentType 设置为 application/octet-stream

    this link can help you

    【讨论】:

      【解决方案2】:

      我解决了这个问题。

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

      这个代码块解决了我的问题

      【讨论】:

        猜你喜欢
        • 2018-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-23
        • 2015-10-21
        相关资源
        最近更新 更多