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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…