2017-05-07 24 views
0

假设我有一个下载URL,当你获取它时,它会下载一个文件。 现在,这个文件不是txt或任何东西,它没有扩展名。 我如何将GET请求编码到URL,但将其下载到某个路径? 编辑:另外,我将如何将其转换为TXT,然后从TXT读取? 注意:这是一个获取请求网站,即时下载文件,而不是在您可以在浏览器中打开的网站上的文件 编辑2:它实际上返回XML,而不是文件,对不起 只是使用浏览器下载它。(C#)如何获取下载网址到某个路径?

+0

的可能的复制[如何下载在C#中的URL文件?(http://stackoverflow.com/questions/307688/how-to-download-a-file-from-a-url -in-c) – theB

+0

不是,不是重复。该URL不需要WebClient.DownloadFile,它只需要GET请求。 – WebCodingFun

+0

@WebCodingFun我的回答有什么问题?如果您喜欢,我会免费提供“如何将文件内容读回内存”位。 –

回答

0

该文件的真实内容是什么?

  1. 您可以尝试将内容类型配置为“application/octet-stream”。 它要求服务器输入字节内容。

  2. 如果内容已经是常规文本,您可以简单地将“.txt”添加到文件名,并且您可以随时阅读。

+0

内容为XML,但下载后没有扩展名。它会下载到浏览器的下载路径中。 – WebCodingFun

+0

阅读新的编辑 – WebCodingFun

0

你这样做,它应该无关紧要,如果你的链接有一个明确的结局,就像我用过的那样。或者如果你真的认真对待GET零件明确使用RestSharp。看看现在你甚至可以从代码中改变文件扩展名,而不是最重要的。自从你提到你的文件是xml以来,我抛弃了一些Linq2Xml,我认为你可能需要对它做些什么。

using System; 
using System.Diagnostics; 
using System.IO; 
using System.Net.Http; 
using System.Xml.Linq; 
using System.Linq; 
using RestSharp; 

namespace Get2File 
{ 
    internal class Program 
    { 
     private const string FallbackUrl = @"https://gist.github.com/Rusk85/8d189cd35295cfbd272d8c2121110e38/raw/4885d9ba37528faab50d9307f76800e2e1121ea2/example-xml-with-embedded-html.xml"; 

     private string _downloadedContent = null; 

     private const string FileNameWithoutExtension = "File"; 

     private static void Main(string[] args) 
     { 
      var p = new Program(); 
      p.Get2FileWithRestSharp(fileExtensions:".xml"); 
      p.UseLinq2XmlOnFile(); 
     } 

     private void Get2File(string altUrl = null, string fileExtensions = ".txt") 
     { 
      var url = !string.IsNullOrEmpty(altUrl) 
       ? altUrl 
       : FallbackUrl; 
      var client = new HttpClient(); 
      var content = client.GetStringAsync(url).Result; 
      _downloadedContent = content; 
      var outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{FileNameWithoutExtension}{fileExtensions}"); 
      File.WriteAllText(outputPath, content); 
     } 

     private void Get2FileWithRestSharp(string altUrl = null, string fileExtensions = ".txt") 
     { 
      var url = !string.IsNullOrEmpty(altUrl) 
       ? altUrl 
       : FallbackUrl; 
      var urlAsUri = new Uri(url); 
      var client = new RestClient(urlAsUri); 
      var request = new RestRequest(Method.GET); 
      var content = string.Empty; 
      var result = client.Execute(request); 
      content = result.Content; 
      _downloadedContent = content; 
      var output = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{FileNameWithoutExtension}{fileExtensions}"); 
      File.WriteAllText(output, content); 
     } 


     private void UseLinq2XmlOnFile() 
     { 
      XElement xElement = XElement.Parse(_downloadedContent); 
      var elements = xElement.Elements(); 
      var StringElement = elements.FirstOrDefault(e => e.Name == "String"); 
      var tranlateXAttribute = StringElement.Attributes().FirstOrDefault(attr => attr.Name == "translate"); 
      Debug.WriteLine(tranlateXAttribute.Value); 
     } 
    } 
}