Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#.net DocuSign Api call return error ( The underlying connection was closed: An unexpected error occurred on a receive).DocuSign URL contain two part (baseURL,ResourcePath). sending document for sign as base64string.see the following code.

What I have tried:

private void Form1_Load(object sender, EventArgs e)
        {           
            String baseURL = "https://prime-dsa-devctr.docusign.net:8081";
            String resourcePath = "sapiws/v1/digital_signature";
            String apiUrl = baseURL + "/" + resourcePath;
            var client = new RestClient(apiUrl);            
            var request = new RestRequest(Method.PUT);
            request.AddHeader("content-type", "application/json");
            request.AddHeader("authorization", "Basic " + DSABasicAuthorizationString("sudeep@cse.com.sa", "####"));
            // "digital_signature" request body as Json formated String (use JavaScriptSerializer or Newtonsoft.Json to build from object) 
            String DigSigRequestBody =
                "{ \"CreateAndSignField\" : " + //structure name specifies the operation / function
                    "{   \"file\": " + "\"" + File2Base64String("C:\\work\\PurchaseOrder.pdf") + "\", " +
                        "\"fileType\": \"PDF\", " +
                        "\"x\": \"91\", " +
                        "\"y\": \"164\", " +
                        "\"width\": \"113\", " +
                        "\"height\": \"38\", " +
                        "\"page\": \"1\", " +
                        "\"timeFormat\": \"h:mm:ss\", " +
                        "\"dateFormat\": \"dd/MM/yyyy\", " +
                        "\"appearance\": [\"GRAPHICAL_IMAGE\", \"SIGNED_BY\", \"TIME\"]" +
                    "}" +
                "}";
            request.AddParameter("application/json", DigSigRequestBody, ParameterType.RequestBody);            
            IRestResponse response = client.Execute(request);            
            JObject joResponse = JObject.Parse(response.Content);

            Base64String2Path(joResponse["signedFile"].Value<string>(), "C:\\work\\PurchaseOrder.DSA-REST-SIGNED.pdf");
            Console.WriteLine("success");
            Console.ReadKey();
        }

private static void Base64String2Path(string Base64String, string FilePath)
        {
            Byte[] bytes = Convert.FromBase64String(Base64String);
            File.WriteAllBytes(FilePath, bytes);
        }

        private static String File2Base64String(String FilePath)
        {
            Byte[] bytes = File.ReadAllBytes(FilePath);
            String fileB64Data = System.Convert.ToBase64String(bytes);
            return fileB64Data;
        }

        private static String DSABasicAuthorizationString(String username, string password)
        {
            var DSABasicAuthorizationBytes = System.Text.Encoding.UTF8.GetBytes(username + ":" + password);
            return System.Convert.ToBase64String(DSABasicAuthorizationBytes);
        }
Posted
Updated 12-Jan-21 19:06pm

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900