Web Sercices : error 400 when sending big file by paquet

I use the Web Services API example to write a program that send big file into ProjectWise.

First an Handshake is send : the return code is 308, it is OK.

Then the file is read by1mo and each package is send until the end of file.

In my case the first package send by PutAsync method  is OK (return code 308)

The second one make a return code 400 that stop the process.

Sometimes it was the third part that makes error 400 !

Have an idea ?

Thanks  you for reply.

Here is the code :

using (var file = uploadFile)

           {

               HttpResponseMessage response;

               do

               {

                   using (var client = new HttpClient())

                   {

                       client.DefaultRequestHeaders.Add("Authorization", String.Format("Basic {0}", encoded));

                       // 1. Send an initial "handshake" request to inform the server of the total

                       //   size of the file that will be uploaded;

                       HttpContent content = new StringContent("");

                       content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

                       content.Headers.ContentDisposition.FileName = fileName;

                       content.Headers.Add("Content-Range", String.Format("bytes */{0}", file.Length));

                       //content.Headers.Add("Content-Range", String.Format("bytes */{0}", 100));

                       response = client.PutAsync(request, content).Result;

                       if (response.StatusCode != (HttpStatusCode)308)

                       {

                           throw (new ArgumentException(response.ToString()));

                         // break; // Either Created or an error, handle outside loop.

                       }

                       int bufferSize = 1024 * 1024; // 1 MB

                       var buffer = new byte[bufferSize];

                       // 2. Send the file:

                        // When the server is still missing some bytes the response will have a status of 308 (Resume Incomplete):

                       while ((response.StatusCode == (HttpStatusCode)308) )

                       {

                           // Response header Range: bytes=0-{endByteIndex} indicates how many bytes the server has received and saved.

                           start = 0;

                           tot = 0;

                           if (response.Headers.Contains("Range")) // No Range header indicates that the server has 0 bytes of the file.

                           {

                               string range = response.Headers.GetValues("Range").First();

                               RangeHeaderValue parsedValue = RangeHeaderValue.Parse(range);

                               start = (long)parsedValue.Ranges.First().To + 1;

                           }

                           file.Position = start;

                           //buffer = new byte[bufferSize]; // ajout DL

                         int readCount = file.Read(buffer, 0, bufferSize);

                           tot = start + readCount;

                           jauge = tot * 100 / file.Length;

                           formProgress.f_Valuejauge((int)jauge);

 

                           // Send a request with the file content and Content-Range: bytes {startByteIndex}-{endByteIndex}/{fileSize} header:

                           content = new ByteArrayContent(buffer, 0, readCount);

                           content.Headers.ContentRange = new ContentRangeHeaderValue(start, start + readCount - 1, file.Length);

                           // A successful response will have an ETag header by which the upload is identified:

                           client.DefaultRequestHeaders.IfMatch.Clear();

                           client.DefaultRequestHeaders.IfMatch.Add(response.Headers.ETag);

                           response = client.PutAsync(request, content).Result;

                           retour = response.ToString();

                       }

                   }

               } while (response.StatusCode == HttpStatusCode.PreconditionFailed); // If If-Match did not match restart the upload process.

               // 3. Handle the final response:

              if (response.StatusCode != HttpStatusCode.OK)

               {

                   // Error handling should be implemented here.

                   throw (new ArgumentException(response.ToString()));

               }

               else

               {

                   string createdObjectIdJson = response.Content.ReadAsStringAsync().Result;

               }

           }