S
S
sidor_tank2019-03-17 13:29:46
API
sidor_tank, 2019-03-17 13:29:46

How to properly translate .docx file for use in HttpWebRequest POST body?

I did it like this but the server returns an error:

using (FileStream fstream = File.OpenRead(dlg.FileName))
                {
                    // преобразуем строку в байты
                    byte[] array = new byte[fstream.Length];
                    // считываем данные
                    fstream.Read(array, 0, array.Length);
                    // декодируем байты в строку
                    content += System.Text.Encoding.Unicode.GetString(array);
                }
...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL_F);
                request.Method = "POST";
                string jsonContent = "{\u0022branch\u0022:\u0022master\u0022, \u0022author_name\u0022:\u0022Sergey\u0022, \u0022content\u0022:\u0022" + content + "\u0022,\u0022commit_message\u0022:\u0022My commit\u0022}";
                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                Byte[] byteArray = encoding.GetBytes(jsonContent);

                request.ContentLength = byteArray.Length;
                request.ContentType = @"application/json";

                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }
                long length = 0;
                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        length = response.ContentLength;
                    }
                }

dlg.FileName Word file name

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CHolfield, 2019-03-17
@sidor_tank

The bytes must be converted to base64 string, passed this string to the server in default encoding, on the base64 server back to the byte array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question