Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

c# - Posting byte array to the server

I am using POST to sent a byte array and a string to the server but is not sucessfull, am I doing the right thing?

memStream.Write(image, 0, signature.Length);, image is a byte array.

Code:

Uri wsHost = new Uri(WebServices.RESTEnpointAddress());
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(wsHost);

                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

                // Boundary
                var boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");

                // Set the request type
                request.ContentType = "multipart/form-data; boundary=" + boundary;
                request.Method = "POST";
                request.KeepAlive = true;
                //request.ContentLength = docByte.Length;

                // Create a new memory stream
                Stream memStream = new MemoryStream();

                // Boundary in bytes
                byte[] boundaryByte = Encoding.ASCII.GetBytes("
--" + boundary + "
");

                // body
                memStream.Write(boundaryByte, 0, boundaryByte.Length);
                string ImgBody = string.Format("Content-Disposition: form-data; name="{0}"; filename="{1}"
", "signImg", "tmpSignImgName");
                ImgBody += "Content-Type: application/octet-stream

";
                byte[] ImgBodyByte = Encoding.ASCII.GetBytes(ImgBody);
                memStream.Write(ImgBodyByte, 0, ImgBodyByte.Length);
                memStream.Write(image, 0, signature.Length); // image ss a byte array
                memStream.Write(boundaryByte, 0, boundaryByte.Length);

                string signLocLatBody = string.Format("Content-Disposition: form-data; name="{0}"

", "signloclat");
                signLocLatBody += latitude;
                byte[] signLocLatBodyByte = Encoding.ASCII.GetBytes(signLocLatBody);
                memStream.Write(signLocLatBodyByte, 0, signLocLatBodyByte.Length);
                memStream.Write(boundaryByte, 0, boundaryByte.Length);

                Stream stream = request.GetRequestStream();
                memStream.Position = 0;
                byte[] tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
                stream.Write(tempBuffer, 0, tempBuffer.Length);
                stream.Close();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
     string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
     byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "
");

     HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(MyUrl);

     webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
     webRequest.Method = "POST";


     using (Stream requestStream = webRequest.GetRequestStream())
     {
              // write boundary bytes
              requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

              // write header bytes.
              string headerTemplate = "Content-Disposition: form-data; name="{0}"; filename="{1}"
Content-Type: {2}

";
              string header = string.Format(headerTemplate, "MyName", "MyFileName", "content type");
              byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
              requestStream.Write(headerbytes, 0, headerbytes.Length);

              using (MemoryStream memoryStream = new MemoryStream(imageBytes))
              {
                      byte[] buffer = new byte[4096];
                      int bytesRead = 0;
                      while ((bytesRead = memoryStream.Read(buffer, 0, buffer.Length)) != 0)
                      {
                              requestStream.Write(buffer, 0, bytesRead);
                      }
               }

               // write trailing boundary bytes.
               byte[] trailerBytes = System.Text.Encoding.ASCII.GetBytes("
--" + boundary + "--
");
               requestStream.Write(trailerBytes, 0, trailerBytes.Length);

       }

       using (HttpWebResponse wr = (HttpWebResponse)webRequest.GetResponse())
       {
               using (Stream response = wr.GetResponseStream())
               {
                    // handle response stream.
               }
       }

This reads a MemoryStream and writes the data to the requestStream, with a 4096 byte buffer. This should be wrapped in a try-catch so it can trap exceptions and handle them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...