我有一个URL(客户端实时反馈的URL),在浏览器中输入后会返回XML响应。我已将其保存在文本文件中,大小为8 MB。
现在我的问题是我需要将这个响应保存在服务器驱动器上的XML文件中。然后我将把它插入到数据库中,并使用C# .NET 4.5的http-client或rest-sharp库的代码发出请求。
对于上述情况,我不确定该怎么做。有人能给我建议吗?
我有一个URL(客户端实时反馈的URL),在浏览器中输入后会返回XML响应。我已将其保存在文本文件中,大小为8 MB。
现在我的问题是我需要将这个响应保存在服务器驱动器上的XML文件中。然后我将把它插入到数据库中,并使用C# .NET 4.5的http-client或rest-sharp库的代码发出请求。
对于上述情况,我不确定该怎么做。有人能给我建议吗?
使用 RestSharp,它就在 readme 中:
var client = new RestClient("http://example.com");
client.DownloadData(request).SaveAs(path);
使用HttpClient
会涉及到更多细节。请看这篇博客文章。
另一个选择是Flurl.Http(免责声明:我是作者)。它在幕后使用了HttpClient
,提供了流畅的接口和许多方便的辅助方法,包括:
await "http://example.com".DownloadFileAsync(folderPath, "foo.xml");
在 NuGet 上获取它。
看起来SaveAs已经停用了。你可以尝试这个。
var client = new RestClient("http://example.com")
byte[] response = client.DownloadData(request);
File.WriteAllBytes(SAVE_PATH, response);
using RestSharp.Extensions;
- C.J.如果您需要异步版本
var request = new RestRequest("/resource/5", Method.GET);
var client = new RestClient("http://example.com");
var response = await client.ExecuteTaskAsync(request);
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception($"Unable to download file");
response.RawBytes.SaveAs(path);
ExecuteTaskAsync
也已经过时了。 - Marc.2377var tempFile = Path.GetTempFileName();
using var writer = File.OpenWrite(tempFile);
var client = new RestClient(baseUrl);
var request = new RestRequest("Assets/LargeFile.7z");
request.ResponseWriter = responseStream =>
{
using (responseStream)
{
responseStream.CopyTo(writer);
}
};
var response = client.DownloadData(request);
使用Bearer身份验证dotnet add package RestSharp
// Download file from 3rd party API
[HttpGet("[action]")]
public async Task<IActionResult> Download([FromQuery] string fileUri)
{
// Using rest sharp
RestClient client = new RestClient(fileUri);
client.ClearHandlers();
client.AddHandler("*", () => { return new JsonDeserializer(); });
RestRequest request = new RestRequest(Method.GET);
request.AddParameter("Authorization", string.Format("Bearer " + accessToken),
ParameterType.HttpHeader);
IRestResponse response = await client.ExecuteTaskAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
// Read bytes
byte[] fileBytes = response.RawBytes;
var headervalue = response.Headers.FirstOrDefault(x => x.Name == "Content-Disposition")?.Value;
string contentDispositionString = Convert.ToString(headervalue);
ContentDisposition contentDisposition = new ContentDisposition(contentDispositionString);
string fileName = contentDisposition.FileName;
// you can write a own logic for download file on SFTP,Local local system location
//
// If you to return file object then you can use below code
return File(fileBytes, "application/octet-stream", fileName);
}
}
使用基本身份验证
// Download file from 3rd party API
[HttpGet("[action]")]
public async Task<IActionResult> Download([FromQuery] string fileUri)
{
RestClient client = new RestClient(fileUri)
{
Authenticator = new HttpBasicAuthenticator("your user name", "your password")
};
client.ClearHandlers();
client.AddHandler("*", () => { return new JsonDeserializer(); });
RestRequest request = new RestRequest(Method.GET);
IRestResponse response = await client.ExecuteTaskAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
// Read bytes
byte[] fileBytes = response.RawBytes;
var headervalue = response.Headers.FirstOrDefault(x => x.Name == "Content-Disposition")?.Value;
string contentDispositionString = Convert.ToString(headervalue);
ContentDisposition contentDisposition = new ContentDisposition(contentDispositionString);
string fileName = contentDisposition.FileName;
// you can write a own logic for download file on SFTP,Local local system location
//
// If you to return file object then you can use below code
return File(fileBytes, "application/octet-stream", fileName);
}
}
SaveAs
被移除了吗?我不能使用它。 可能我需要使用File.WriteAllBytes(fileName, bytesArray);
。 - JCarlosRSaveAs
方法仍然存在,位于RestSharp.Extensions
命名空间的MiscExtensions
类中。 - pushasha