我正在开发一套ASP.Net Web API,将第三方文档管理系统集成到现有的CMS中。第三方有一组API调用来启用此功能。我已经完成了大部分集成工作,但下载文档非常慢。这看起来是因为我打电话下载。该文件从第三方下载到Web服务器,然后从Web服务器下载到客户端。
出于安全原因,我试图避免直接调用第三方API。有没有办法让我的端点充当第三方API的代理。允许文件流直接传递给客户端吗?
public static HttpResponseMessage GetDocument(string docId, string docName)
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = getDocumentStream(docId, docName);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.Add("Content-Disposition", "attachment; filename=\"" + docName + "\"");
return result;
}
public static Stream getDocumentStream(string docId,string name)
{
var UriTemplate = ApiUri;
var ParamTemplate = "name=" + name + "&documentId=" + docId;
var userName = ConfigurationManager.AppSettings["EdeskUserName"];
var passWord = ConfigurationManager.AppSettings["EdeskPWD"];
return EDeskDataUtility.EdeskAPIDownload(UriTemplate, ParamTemplate, userName, passWord);
}
public static Stream DownloadFile(string URI, string Parameters)
{
WebResponse resp = GetURIResponse(URI, Parameters);
if (resp == null) return null;
return resp.GetResponseStream();
}
public static System.Net.WebResponse GetURIResponse(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
var resp = req.GetResponse();
return resp;
}