3

POSTを使用してバイト配列と文字列をサーバーに送信していますが、成功していません。正しいことをしていますか?

memStream.Write(image、0、signature.Length);、imageはバイト配列です。

コード:

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("\r\n--" + boundary + "\r\n");

                // body
                memStream.Write(boundaryByte, 0, boundaryByte.Length);
                string ImgBody = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n", "signImg", "tmpSignImgName");
                ImgBody += "Content-Type: application/octet-stream\r\n\r\n";
                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}\"\r\n\r\n", "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();
4

3 に答える 3

4
     string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
     byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");

     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}\"\r\nContent-Type: {2}\r\n\r\n";
              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("\r\n--" + boundary + "--\r\n");
               requestStream.Write(trailerBytes, 0, trailerBytes.Length);

       }

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

これにより、MemoryStreamが読み取られ、4096バイトのバッファーを使用してデータがrequestStreamに書き込まれます。これは、例外をトラップして処理できるように、try-catchでラップする必要があります。

于 2012-10-19T11:04:35.813 に答える
3

WebRequest次のようにデータを投稿するために使用します。

WebRequest request = WebRequest.Create ("MyURL"); 
request.Method = "POST"; 
string postData = "This is a test that posts this string to a Web server.";
request.ContentType = "image/jpeg"; 
request.ContentLength = byteArray.Length; 


//Here is the Business end of the code... 
Stream dataStream = request.GetRequestStream (); 
dataStream.Write (byteArray, 0, byteArray.Length); 
dataStream.Close (); 
//and here is the response. 
WebResponse response = request.GetResponse (); 

//Writing response from server

dataStream = response.GetResponseStream (); 
StreamReader reader = new StreamReader (dataStream); 
string responseFromServer = reader.ReadToEnd (); 
Console.WriteLine (responseFromServer); 
reader.Close (); 
dataStream.Close (); 
response.Close (); 
于 2012-10-19T11:05:08.957 に答える
0

リクエストストリームへの参照としてmemStreamを使用する必要があります。

Stream memStream = request.GetRequestStream();

次に、それに書き込むときは、リクエストに書き込んでいます。

于 2012-10-19T10:36:30.167 に答える