[英]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
我有一张图片,我将此图片转换为字节 [],如果您想查看我的代码,我确实使用 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);
//}
}
And will be share with you my rest service you are looking my code if you want ->并将与您分享我的 rest 服务,如果需要,您正在查看我的代码 ->
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.我的 byte[] 长度很大,但我只取了 34 个。我尝试了很多方法。 Please help me.
请帮我。
application/json
is for communicating with JSON encoding, byte array is not a json object. application/json
用于与 JSON 编码进行通信,字节数组不是 json object。 you need to set your httpRequest.Accept
and httpRequest.ContentType
to application/octet-stream
您需要将
httpRequest.Accept
和httpRequest.ContentType
设置为application/octet-stream
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这个代码块解决了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.