Examples

The examples below describe the execution of requests using Windows and ABBYY FlexiCapture authentication.

To view the description of parameters as well as additional information on how to use POST requests with files, consult with Working with files.

Loading files from server

public static async Task<Stream> LoadFileFromServer(Uri server, string login, string password,
   int objectType, int sessionId, int projectId, int batchId, int parentId, int objectId,
   int version, string streamName)
{
   var creds = CredentialCache.DefaultNetworkCredentials;
   var auth = "";
   if (!string.IsNullOrEmpty(login))
   {
       creds = new NetworkCredential(login, password);
       auth = "FCAuth/";
   }
   using (var handler = new HttpClientHandler { Credentials = creds })
   using (var client = new HttpClient(handler))
   {
       var uri = new Uri(server, "FlexiCapture12/Server/" + auth + "FileService/v1");
       var content = new FormUrlEncodedContent(new Dictionary<string, string>
               {
                   {"Action", "Load"},
                   {"objectType", objectType.ToString()},
                   {"sessionId", sessionId.ToString()},
                   {"projectId", projectId.ToString()},
                   {"batchId", batchId.ToString()},
                   {"parentId", parentId.ToString()},
                   {"objectId", objectId.ToString()},
                   {"version", version.ToString()},
                   {"streamName", streamName},
               });
       var msg = await client.PostAsync(uri, content);
       msg.EnsureSuccessStatusCode();
       return await msg.Content.ReadAsStreamAsync();
   }
}

Uploading files to server

public static async Task<string> UploadFileToServer(Uri server, string login, string password,
   string filename, int objectType, int sessionId, int projectId, int batchId, int parentId,
   int objectId, int version, string streamName)
{
   var creds = CredentialCache.DefaultNetworkCredentials;
   var auth = "";
   if (!string.IsNullOrEmpty(login))
   {
       creds = new NetworkCredential(login, password);
       auth = "FCAuth/";
   }
   using (var handler = new HttpClientHandler { Credentials = creds })
   using (var client = new HttpClient(handler))
   {
       using (var fs = File.OpenRead(filename))
       {
           var uri = new Uri(server, "FlexiCapture12/Server/" + auth + "FileService/v1");
           var encodedStreamName = Convert.ToBase64String(Encoding.Unicode.GetBytes(streamName));
           var content = new MultipartFormDataContent
                   {
                       {new StringContent("Save"), "Action"},
                       {new StringContent(objectType.ToString()), "objectType"},
                       {new StringContent(sessionId.ToString()), "sessionId"},
                       {new StringContent(projectId.ToString()), "projectId"},
                       {new StringContent(batchId.ToString()), "batchId"},
                       {new StringContent(parentId.ToString()), "parentId"},
                       {new StringContent(objectId.ToString()), "objectId"},
                       {new StringContent(version.ToString()), "version"},
                       {new StringContent(encodedStreamName), "streamName"},
                       {new StreamContent(fs), "blob", "data.txt"}
                   };
           var msg = await client.PostAsync(uri, content);
           msg.EnsureSuccessStatusCode();
           return await msg.Content.ReadAsStringAsync();
       }
   }
}

Uploading chunked files to server

In the example below, the file is being uploaded to the server in parts of 64 KB.

public static async Task UploadChunkedFileToServer(Uri server, string login, string password,
   string filename, int objectType, int sessionId, int projectId, int batchId, int parentId,
   int objectId, int version, string streamName)
{
   var creds = CredentialCache.DefaultNetworkCredentials;
   var auth = "";
   if (!string.IsNullOrEmpty(login))
   {
       creds = new NetworkCredential(login, password);
       auth = "FCAuth/";
   }
   using (var handler = new HttpClientHandler { Credentials = creds })
   using (var client = new HttpClient(handler))
   {
       using (var fs = File.OpenRead(filename))
       {
           var offset = 0;
           var uri = new Uri(server, "FlexiCapture12/Server/" + auth + "FileService/v1");
           var buffer = new byte[0x10000];
           var readed = fs.Read(buffer, 0, buffer.Length);
           while (readed > 0)
           {
               var action = offset == 0 ? "BeginSaveChunked" : "Append";

               var encodedStreamName = Convert.ToBase64String(Encoding.Unicode.GetBytes(streamName));
               var content = new MultipartFormDataContent
                       {
                           {new StringContent(action), "Action"},
                           {new StringContent(objectType.ToString()), "objectType"},
                           {new StringContent(sessionId.ToString()), "sessionId"},
                           {new StringContent(projectId.ToString()), "projectId"},
                           {new StringContent(batchId.ToString()), "batchId"},
                           {new StringContent(parentId.ToString()), "parentId"},
                           {new StringContent(objectId.ToString()), "objectId"},
                           {new StringContent(version.ToString()), "version"},
                           {new StringContent(encodedStreamName), "streamName"},
                           {new StringContent(offset.ToString()), "offset"},
                           {new ByteArrayContent(buffer, 0, readed), "blob", "data.txt"}
                       };
               if (offset > 0) content.Add(new StringContent(offset.ToString()), "offset");
               var msg = await client.PostAsync(uri, content);
               msg.EnsureSuccessStatusCode();
               var str = await msg.Content.ReadAsStringAsync();

               offset += readed;
               readed = fs.Read(buffer, 0, buffer.Length);
           }
       }
   }
}

Getting checksum

The FlexiCapture server returns a checksum of the CRC-32-IEEE 802.3 type.

public static async Task<string> GetCrc(Uri server, string login, string password,
   int objectType, int sessionId, int projectId, int batchId, int parentId,
   int objectId, int version, string streamName)
{
   var creds = CredentialCache.DefaultNetworkCredentials;
   var auth = "";
   if (!string.IsNullOrEmpty(login))
   {
       creds = new NetworkCredential(login, password);
       auth = "FCAuth/";
   }
   using (var handler = new HttpClientHandler { Credentials = creds })
   using (var client = new HttpClient(handler))
   {
       var uri = new Uri(server, "FlexiCapture12/Server/" + auth + "FileService/v1");
       var content = new FormUrlEncodedContent(new Dictionary<string, string>
               {
                   {"Action", "Checksum"},
                   {"objectType", objectType.ToString()},
                   {"sessionId", sessionId.ToString()},
                   {"projectId", projectId.ToString()},
                   {"batchId", batchId.ToString()},
                   {"parentId", parentId.ToString()},
                   {"objectId", objectId.ToString()},
                   {"version", version.ToString()},
                   {"streamName", streamName},
               });
       var msg = await client.PostAsync(uri, content);
       msg.EnsureSuccessStatusCode();
       return await msg.Content.ReadAsStringAsync();
   }
}

4/12/2024 6:16:01 PM

Please leave your feedback about this article

Usage of Cookies. In order to optimize the website functionality and improve your online experience ABBYY uses cookies. You agree to the usage of cookies when you continue using this site. Further details can be found in our Privacy Notice.