-2

I just got some Java code which i need to convert to C# and upload the file into Documentum server. I Converted into C#, need to have some queries.

what is the boundary (In Java using some constant unique value) what to declare in C#,And i hope "a_content_type" and "object_name" are added headers. what is the SetTimeOut,ReadTimeOut,UseCatches,DoInput and DoOutput in C#?

Java Code:

private HttpURLConnection getHttpURLConnection( String fileName, String  fileExt, URL urlOne ) throws IOException            
    HttpURLConnection connection;
    connection = ( HttpURLConnection ) urlOne.openConnection();
    connection.setRequestMethod( "POST" );
    connection.setRequestProperty( CONTENT_TYPE, "multipart/form- 
    data;boundary="+ BOUNDARY );
    connection.setRequestProperty( ACCEPT, "application/vnd.emc.documentum+json" );
    connection.setRequestProperty( AUTHORIZATION,BASIC+ 
    Base64.encodeBase64String( ( username + ":" + password ) .getBytes() ) );
    connection.setUseCaches( false );
    connection.setRequestProperty( "Object_Name", "test_pdffile");
    connection.setRequestProperty( A_CONTENT_TYPE, "pdf" );
    connection.setDoInput( true );
    connection.setDoOutput( true );    
    connection.setConnectTimeout(TIMEOUT * THOUSAND);
    connection.setReadTimeout(TIMEOUT * THOUSAND);
    return connection;
}

C# Code:

try 
{
     System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
     request.Method = "GET";
     request.ContentType = "multipart/form-data;boundary="+ BOUNDARY;
     request.PreAuthenticate = true;         
     String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
     request.Headers.Add("Authorization", "Basic " + encoded);
     request.Accept = "application/vnd.emc.documentum+json";
     request.Headers["Object_Name"] = "test_pdffile";
     request.Headers["a_content_type"] = "pdf";
}
AdithyaM
  • 59
  • 1
  • 2
  • 10
  • How on earth do you expect us to know the value of arbitrary constants defined in your application? Why don't you just, I dunno, **use the values of those constants**? – Ian Kemp May 03 '19 at 04:20

2 Answers2

0

Here is the code i have used. It is working.

D2Document newDocument = new D2Document();
newDocument.SetPropertyValue("object_name", fileName);
newDocument.SetPropertyValue("a_content_type", contenType);
String documentURL = ConfigurationManager.AppSettings["DOCUMENTUM_URL"] + "objects/"+ documentId + "/content-media?format=" + contenType + "&modifier=&page=0";
JSON_GENERIC_MEDIA_TYPE = new MediaTypeWithQualityHeaderValue("application/json");
JSON_VND_MEDIA_TYPE = new MediaTypeWithQualityHeaderValue("application/vnd.emc.documentum+json");
try
    {
        using (var multiPartStream = new MultipartFormDataContent())
            {
                MemoryStream stream = new MemoryStream();
                JsonSerializer.WriteObject(stream, newDocument);
                ByteArrayContent firstPart = new ByteArrayContent(stream.ToArray());
                firstPart.Headers.ContentType = JSON_VND_MEDIA_TYPE;               
               multiPartStream.Add(firstPart);
               stream.Dispose();
               HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, documentURL);
               request.Content = multiPartStream;
               String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
               request.Headers.Add("Authorization", "Basic " + encoded);                      
               using (HttpResponseMessage response = _httpClient.GetAsync(documentURL).Result)
                  {
                     if (response != null)
                        {
                           var responsestream = response.Content;
                         }}}}
AdithyaM
  • 59
  • 1
  • 2
  • 10
-1

For the boundary - look here

"a_content_type" and "object_name" are indeed headers
SetTimeOut is: request.Timeout
ReadTimeOut is request.ReadWriteTimeout

UseCatches,DoInput and DoOutput are for Java specific uses that don't have any equivalent in C# as i know.

  • Thanks so much! Appreciate. any idea how do we use BOUNDARY in C#? – AdithyaM May 03 '19 at 15:26
  • NP, please refer to https://ec.haxx.se/http-multipart.html in order to learn more about the boundary part as it's not a c# specific matter but an. if my answer helped you, please mark it as the accepted answer. – Oded Dahari May 05 '19 at 01:04