gpt4 book ai didi

c# - 无法使用 RestSharp 下载 pdf?

转载 作者:行者123 更新时间:2023-12-01 17:43:18 25 4
gpt4 key购买 nike

我一直在努力下载使用restsharp在线托管的简单pdf。我已经研究了一个多小时的代码,得到的只是空对象结果。

使用 GET 在 POSTMAN 中轻松下载该文件,并且没有设置内容 header ,但仍然给出了什么?

下面是我一直在尝试的 noddy 沙箱测试:

[TestFixture]
public class Sandbox
{
[Test]
public void Test()
{
var uri = "https://www.nlm.nih.gov/mesh/2018/download/2018NewMeShHeadings.pdf";
var client = new RestClient();
var request = new RestRequest(uri, Method.GET);
//request.AddHeader("Content-Type", "application/octet-stream");
byte[] response = client.DownloadData(request);
File.WriteAllBytes(@"C:\temp\1.pdf", response);
}
}

更新: Return a Stream

        var baseUri = "https://www.nlm.nih.gov/mesh/2018/download/";
var client = new RestClient(baseUri);
var request = new RestRequest("2018NewMeShHeadings.pdf", Method.GET);
request.AddHeader("Content-Type", "application/octet-stream");
var tempFile = Path.GetTempFileName();
var stream = File.Create(tempFile, 1024, FileOptions.DeleteOnClose);
request.ResponseWriter = responseStream => responseStream.CopyTo(stream);
var response = client.DownloadData(request);

该流现在已填充有下载的数据。

最佳答案

试试这个:

    var uri = "https://www.nlm.nih.gov/mesh/2018/download/";
var client = new RestClient(uri);
var request = new RestRequest("2018NewMeShHeadings.pdf", Method.GET);
//request.AddHeader("Content-Type", "application/octet-stream");
byte[] response = client.DownloadData(request);

关于c# - 无法使用 RestSharp 下载 pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47099705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com