DocuSign - RestApi v2 - 使用C#下载文档

时间:2017-05-04 19:17:33

标签: c# docusignapi

我正在尝试使用以下代码通过RestAPI v2检索已签名的文档。

url = baseURL + "/accounts/" + "3602fbe5-e11c-44de-9e04-a9fc9aa2aad6" + "/envelopes/" + envId + "/documents/combined";
HttpWebRequest request4 = (HttpWebRequest)WebRequest.Create(url);
request4.Method = "GET";
request4.Headers.Add("X-DocuSign-Authentication", authHeader);
request4.Accept = "application/pdf";
request4.ContentType = "application/json";
request4.ContentLength = 0;
HttpWebResponse webResponse4 = (HttpWebResponse)request4.GetResponse();

StreamReader objSR = new StreamReader(webResponse4.GetResponseStream());

StreamWriter objSW = new StreamWriter(@"C:\Users\reddy\Desktop\Docusign\test_" + envId + ".pdf");
objSW.Write(objSR.ReadToEnd());
objSW.Close();    
objSR.Close();

使用上面的代码,我可以保存PDF文件,但有些事情是不对的。有人可以帮我修复我的错误代码。

下载文件: Downloaded Document

原始文件: Original document

1 个答案:

答案 0 :(得分:2)

选项1 :您只需使用System.Net.WebClient

即可
string url = baseURL + "/accounts/" + accountId + "/envelopes/" + envId + "/documents/combined";
string path = @"C:\Users\reddy\Desktop\Docusign\test_" + envId + ".pdf";
using (var wc = new System.Net.WebClient())
{
    wc.Headers.Add("X-DocuSign-Authentication", authHeader);
    wc.DownloadFile(url, path);
}

选项2 :将输入流复制到输出FileStream

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("X-DocuSign-Authentication", authHeader);

using (var response = (HttpWebResponse)request.GetResponse())
{
    using (var stream = File.Create(path))
        response.GetResponseStream().CopyTo(stream);
}

选项3 :使用DocuSign C# SDK

请参阅完整代码here

var envApi = new EnvelopesApi();
var docStream = envApi.GetDocument(accountId, envelopeId, "combined");
using (var stream = File.Create(filePath))
    docStream.CopyTo(stream);

另见answer