# Using SDS using C# and the .NET 4.5 HttpClient

Note

The standalone SDS service described here is typically used together with our DocumentService and PackagingService SOAP APIs.

If you are using our RESTful Sign API, we recommend using the Sign API's built-in Session Data Storage.

If you are an existing customer using DocumentService v3 and PackagingService v4 and have recently migrated to our new platform, the document is uploaded to our Sign API's built-in Session Data Storage (SDS) by using the documents endpoint (opens new window).

# Uploading a document

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Signicat.Support.SDS.Test
{
    [TestClass]
    public class SDSTest
    {
        [TestMethod]
        public async Task Uploading_a_PDF_document_to_SDS()
        {
            var httpClientHandler = new HttpClientHandler { Credentials = new NetworkCredential("demo", "Bond007") };
            using (var client = new HttpClient(httpClientHandler))
            {
                HttpContent content = new ByteArrayContent(File.ReadAllBytes("mydocument.pdf"));
                content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                HttpResponseMessage response =
                    await client.PostAsync("https://preprod.signicat.com/doc/demo/sds", content);
                string documentId = await response.Content.ReadAsStringAsync();

                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
                Assert.IsTrue(documentId.Length > 0);
            }
        }
    }
}

# Downloading a document

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Signicat.Support.SDS.Test
{
    [TestClass]
    public class SDSTest
    {
        [TestMethod]
        public async Task Downloading_a_PDF_document_from_SDS()
        {
            var httpClientHandler = new HttpClientHandler { Credentials = new NetworkCredential("demo", "Bond007") };
            using (var client = new HttpClient(httpClientHandler))
            {
                HttpResponseMessage response =
                    await client.GetAsync("https://preprod.signicat.com/doc/demo/sds/26082013589fl9ppby4c7ltrgyp11kx41bc9mikazp2yhcsk11fgxfgxtd");
                byte[] pdf = await response.Content.ReadAsByteArrayAsync();
                File.WriteAllBytes("mydownloadedfile.pdf", pdf);

                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                Assert.AreEqual("application/pdf", response.Content.Headers.ContentType.ToString());
            }
        }
    }
}

# Using System.Net.WebClient

The examples above is in C# and uses the async syntax and HttpClient in .NET 4.5. Alternatively, you can use the System.Net.Webclient (opens new window)

using (var webClient = new WebClient())
{
    webClient.Credentials = new NetworkCredential("demo", "Bond007");
    byte[] document = webClient.DownloadData(urlToDownload);
}
Last updated: 17/11/2023 10:07 UTC