无法使用RestSharp下载pdf? [英] Cannot download a pdf with RestSharp?
本文介绍了无法使用RestSharp下载pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在努力下载使用restsharp在线托管的简单pdf文件.我已经在代码上玩了一个多小时,而我得到的只是空对象结果.
I have been struggling to download a simple pdf hosted online using restsharp. I have been playing around with the code for over an hour and all I get are null object results.
使用GET轻松在POSTMAN中下载文件,并且没有设置内容标头,但是仍然可以提供什么?
The file downloads easily in POSTMAN using a GET and no content header set but still what gives?
以下是我一直在尝试的点头沙盒"测试:
Below is the noddy sandbox test I have been experimenting around with:
[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);
}
}
更新: 返回流
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);
现在,已用下载的数据填充流.
The stream is now populated with the downloaded data.
推荐答案
尝试一下:
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);
这篇关于无法使用RestSharp下载pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文