使用GET请求跨2个API下载PDF [英] Downloading PDF across 2 APIs using GET request
问题描述
我正在尝试通过GET请求下载跨越两个API的PDF文件.
Im trying to download a PDF file crossing over two API's with a GET Request.
如果我直接使用API2,则可以使用以下代码很好地下载PDF:
If I go direct to API2 the PDF downloads fine with the below code:
Stream fileStream = File.Open(fileLocation, FileMode.Open);
result.Content = new StreamContent(fileStream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test.pdf"
};
return result;
但是,当我将API1放入混音中时,事情会变得有点不可思议!
However when I throw API1 into the mix things get a little wonky!!
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Method = HttpMethod.Get;
httpRequestMessage.RequestUri = new Uri(requestUrl);
HttpResponseMessage response = await client.SendAsync(httpRequestMessage);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var content = await response.Content.ReadAsStringAsync();
response.Content = new StringContent(content);
response.EnsureSuccessStatusCode();
response.Content.Headers.ContentEncoding.Add("UTF8");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test.pdf"
};
return response;
直接进入API2会产生:%PDF-1.6%âãÏÓ
Going direct to API2 produces: %PDF-1.6%âãÏÓ
通过API2生成:%PDF-1.6%
Going via API2 produces:%PDF-1.6%����
我曾经尝试更改API1上的ContentType和ContentEncoding,但没有任何乐趣.
Ive tried changing ContentType and ContentEncoding on API1 with no joy.
有什么东西跳出来吗?
推荐答案
在二进制文档上调用 .ReadAsStringAsync
无法正常工作-您必须调用 .ReadAsByteArrayAsync
.您还必须使用 ByteArrayContent
而不是 StringContent
.
Calling .ReadAsStringAsync
on a binary document wont work - you have to call .ReadAsByteArrayAsync
.
You also have to use ByteArrayContent
instead of StringContent
.
未经测试
这篇关于使用GET请求跨2个API下载PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!