Examples

In this section, you will find the following POST request examples for working with files:

Loading files from the server

Uploading files to the server

Getting a checksum

Loading by parts (modifying an existing file)

Loading files from the server

To load a file from the server, a server request should be created (AppServerURL + "/FlexiCapture12/Server/FileService/v1"). The request method should be POST, and the contents should be of the application/x-www-form-urlencoded type. The results should be recorded to a new file.

Request body:

"Action=Load" +"&sessionId=" + session + "&objectType=" + objectType + "&projectId=" + projectId + "&batchId=" + batchId + "&objectId=" + objectId + "&version=" + version + "&streamName=" + streamName
static private CredentialCache basicAuthenticationCredentialCache = null;
public static List<byte> POSTLoadFile(string AppServerURL, int session, int projectId, int batchId, int objectType, int objectId, int version, int parentId, string streamName)
      {
           List<byte> result = new List<byte>();
           HttpWebResponse response = null;
           Stream responseStream = null;
           BinaryReader reader = null;
           try
           {
               // creating the request
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AppServerURL + "/FlexiCapture12/Server/FileService/v1");
               request.Method = "POST";
               // authentication set up when connecting to the web service
               if (basicAuthenticationCredentialCache != null)
               {
                   request.Credentials = basicAuthenticationCredentialCache;
               }
               else
               {
                   request.UseDefaultCredentials = true;
               }
               request.ContentType = "application/x-www-form-urlencoded";
               string postData = "Action=Load" +
                   "&sessionId=" + session +
                   "&objectType=" + objectType +
                   "&projectId=" + projectId +
                   "&batchId=" + batchId +
                   "&parentId=" + parentId +
                   "&objectId=" + objectId +
                   "&version=" + version +
                   "&streamName=" + streamName;
               StreamWriter writer = new StreamWriter(request.GetRequestStream());
               writer.Write(postData);
               writer.Close();                  
         
               // receiving a response
               response = (HttpWebResponse)request.GetResponse();
               responseStream = response.GetResponseStream();
               reader = new BinaryReader(responseStream);
               int responseStreamLength = 0;
               while (true)
               {
                   try
                   {
                       result.Add(reader.ReadByte());
                       responseStreamLength++;
                   }
                   catch (EndOfStreamException)
                   {
                       break;
                   }
               }
               reader.Close();
               responseStream.Close();
               response.Close();              
           }
           if (response != null) response.Close();
           if (responseStream != null) responseStream.Close();
           if (reader != null) reader.Close();
           return result;
       }

Uploading files to the server

You need to open an existing file and read data from it (The "content" string should be obtained from the binary file using Encoding.Default).

To save the file, a server request should be created (AppServerURL + "/FlexiCapture12/Server/FileService/v1"). The request method should be POST, and the contents should be of the multipart/form-data type.

Request body:

"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"Action\"\r\n\r\nSave" +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectType\"\r\n\r\n" + objectType +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"sessionId\"\r\n\r\n" + session +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"projectId\"\r\n\r\n" + projectId +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"batchId\"\r\n\r\n" + batchId +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectId\"\r\n\r\n" + objectId +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"version\"\r\n\r\n" + version +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"streamName\"\r\n\r\n" + streamNameInBase64 +            // name of the binary stream in Base64
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"blob\";filename=\"filename.txt\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n"+                                       // the value of the filename parameter does not affect the request itself
content +                                                                        //data that is uploaded to the server
"\r\n--" + requestId + "--\r\n";

static private CredentialCache basicAuthenticationCredentialCache = null;
// the "content" string should be obtained from the binary file using Encoding.Default
       public static List<byte> POSTSaveFile(string AppServerURL, int session, int projectId, int batchId, int objectType, int objectId, int version, string streamName, string content)
       {
           List<byte> result = new List<byte>();
           HttpWebResponse response = null;
           Stream responseStream = null;
           StreamReader reader = null;
           int requestId = Environment.TickCount;
           try
           {
               // creating the request
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AppServerURL + "/FlexiCapture12/Server/FileService/v1");
               request.Method = "POST";
               // authentication set up when connecting to the web service
               if (basicAuthenticationCredentialCache != null)
               {
                   request.Credentials = basicAuthenticationCredentialCache;
               }
               else
               {
                   request.UseDefaultCredentials = true;
               }
               request.ContentType = "multipart/form-data, boundary=" + requestId;
               byte[] streamNameInBytes = Encoding.Unicode.GetBytes(streamName);
               UnicodeEncoding UTFencofing = new UnicodeEncoding();
               string postData = "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"Action\"\r\n\r\nSave" +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectType\"\r\n\r\n" + objectType +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"sessionId\"\r\n\r\n" + session +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"projectId\"\r\n\r\n" + projectId +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"batchId\"\r\n\r\n" + batchId +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectId\"\r\n\r\n" + objectId +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"version\"\r\n\r\n" + version +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"streamName\"\r\n\r\n" + Convert.ToBase64String(streamNameInBytes) +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"blob\";filename=\"filename.txt\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n";
               postData += content;
               postData += "\r\n--" + requestId + "--\r\n";
               StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.Default);
               writer.Write(postData);
               writer.Close();  

               // receiving a response
               response = (HttpWebResponse)request.GetResponse();
               responseStream = response.GetResponseStream();
               reader = new StreamReader(responseStream);
               reader.Close();
               responseStream.Close();
               response.Close();
           }
           if (response != null) response.Close();
           if (responseStream != null) responseStream.Close();
           if (reader != null) reader.Close();
           return result;
       }

Getting a checksum

To get a checksum, a server request should be created (AppServerURL + "/FlexiCapture12/Server/FileService/v1"). The request method should be POST, and the contents should be of the application/x-www-form-urlencoded type.

Request body:

"Action=Checksum" + "&sessionId=" + session + "&objectType=" + objectType + "&projectId=" + projectId + "&batchId=" + batchId + "&objectId=" + objectId + "&version=" + version + "&streamName=" + streamName

static private CredentialCache basicAuthenticationCredentialCache = null;
public static List<byte> POSTGetChecksum(string AppServerURL, int session, int projectId, int batchId, int objectType, int objectId, int version, string streamName)
       {
  List<byte> result = new List<byte>();
  HttpWebResponse response = null;
  Stream responseStream = null;
  BinaryReader reader = null;
  try
           {
               // creating the request
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AppServerURL + "/FlexiCapture12/Server/FileService/v1");
               request.Method = "POST";
               // authentication set up when connecting to the web service
   if (basicAuthenticationCredentialCache != null)
   {
    request.Credentials = basicAuthenticationCredentialCache;
   }
   else
   {
    request.UseDefaultCredentials = true;
   }
   request.ContentType = "application/x-www-form-urlencoded";
   string postData = "Action=Checksum" +
    "&sessionId=" + session +
    "&objectType=" + objectType +
    "&projectId=" + projectId +
    "&batchId=" + batchId +
    "&objectId=" + objectId +
    "&version=" + version +
    "&streamName=" + streamName;
   StreamWriter writer = new StreamWriter(request.GetRequestStream());
   writer.Write(postData);
   writer.Close();

               // receiving a response
   response = (HttpWebResponse)request.GetResponse();
   responseStream = response.GetResponseStream();
   reader = new BinaryReader(responseStream);
   int responseStreamLength = 0;
   while (true)
   {
    try
    {
     result.Add(reader.ReadByte());
     responseStreamLength++;
    }
    catch (EndOfStreamException)
    {
     break;
    }
   }
   reader.Close();
   responseStream.Close();
   response.Close();

  }
  if (response != null) response.Close();
  if (responseStream != null) responseStream.Close();
  if (reader != null) reader.Close();
  return result;
 }

Loading by parts (modifying an existing file)

Important! Only a file that already exists on the server can be modified.

In order to modify the file, a server request should be created (AppServerURL + "/FlexiCapture12/Server/FileService/v1"). The request method should be POST, and the contents should be of the multipart/form-data type.

Request body:

"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"Action\"\r\n\r\nAppend" +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectType\"\r\n\r\n" + objectType +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"sessionId\"\r\n\r\n" + session +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"projectId\"\r\n\r\n" + projectId +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"batchId\"\r\n\r\n" + batchId +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectId\"\r\n\r\n" + objectId +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"version\"\r\n\r\n" + version +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"streamName\"\r\n\r\n" + streamNameInBase64 +
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"blob\";filename=\"filename.txt\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" +
сontent +                                                                                                                              //this string should be obtained the same way as in the previous example
"\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"offset\"\r\n\r\n" + offset +            //the offset from the beginning of the file, where the entry starts
"\r\n--" + requestId + "--\r\n";

static private CredentialCache basicAuthenticationCredentialCache = null;
// the "content" string should be obtained from the binary file using Encoding.Default
       // Note: you can only add to a file that already exists, meaning that you must first create a file using the POSTSaveFile method
       public static List<byte> POSTAppendFile(string AppServerURL, int session, int projectId, int batchId, int objectType, int objectId, int version, string streamName, string content, int offset)
       {
           List<byte> result = new List<byte>();
           HttpWebResponse response = null;
           Stream responseStream = null;
           StreamReader reader = null;
           int requestId = Environment.TickCount;
           try
           {
               // creating a request
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AppServerURL + "/FlexiCapture12/Server/FileService/v1");
               request.Method = "POST";
               // authentication set up when connecting to the web service
               if (basicAuthenticationCredentialCache != null)
               {
                   request.Credentials = basicAuthenticationCredentialCache;
               }
               else
               {
                   request.UseDefaultCredentials = true;
               }
               request.ContentType = "multipart/form-data, boundary=" + requestId;
               byte[] streamNameInBytes = Encoding.Unicode.GetBytes(streamName);
               UnicodeEncoding UTFencofing = new UnicodeEncoding();
               string postData = "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"Action\"\r\n\r\nAppend" +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectType\"\r\n\r\n" + objectType +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"sessionId\"\r\n\r\n" + session +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"projectId\"\r\n\r\n" + projectId +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"batchId\"\r\n\r\n" + batchId +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"objectId\"\r\n\r\n" + objectId +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"version\"\r\n\r\n" + version +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"streamName\"\r\n\r\n" + Convert.ToBase64String(streamNameInBytes) +
                   "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"blob\";filename=\"filename.txt\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n";
               postData += content;
               postData += "\r\n--" + requestId + "\r\ncontent-disposition: form-data; name=\"offset\"\r\n\r\n" + offset;
               postData += "\r\n--" + requestId + "--\r\n";
               StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.Default);
               writer.Write(postData);
               writer.Close();

               // receiving a response
               response = (HttpWebResponse)request.GetResponse();
               responseStream = response.GetResponseStream();

               // displaying the response
               reader = new StreamReader(responseStream);
               reader.Close();
               responseStream.Close();
               response.Close();
           }
           if (response != null) response.Close();
           if (responseStream != null) responseStream.Close();
           if (reader != null) reader.Close();
           return result;
       }

1/16/2023 10:02:51 AM

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.