How to use restsharp to download file

China☆狼群 提交于 2019-12-18 03:48:39

问题


I have a URL (URL for the live feed from client) which when I hit in browser returns the xml response . I have saved this in text file it`s size is 8 MB.

now my problem is that I need to save this response in xml file on server`s drive. from there I will insert this in database. and request needs to be made using code using http-client or rest-sharp library of c# .net 4.5

I am unsure what should I do for above case. can any body suggest me something


回答1:


With RestSharp, it's right there in the readme:

var client = new RestClient("http://example.com");
client.DownloadData(request).SaveAs(path);

With HttpClient, it's a bit more involved. Have a look at this blog post.

Another option is Flurl.Http (disclaimer: I'm the author). It uses HttpClient under the hood and provides a fluent interface and lots of convenient helper methods, including:

await "http://example.com".DownloadFileAsync(folderPath, "foo.xml");

Get it on NuGet.




回答2:


It seems SaveAs was discontinued. You can try this

var client = new RestClient("http://example.com")    
byte[] response = client.DownloadData(request);
File.WriteAllBytes(SAVE_PATH, response);



回答3:


In case you want async version

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);


来源:https://stackoverflow.com/questions/29123291/how-to-use-restsharp-to-download-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!