So let's say I have a download url that when you GET it, it downloads a file. Now, this file is not a txt or anything, it has no extension. How would I code a GET request to the URL, but make it download to a certain path? EDIT: Also, how would I convert it to a TXT and read from the txt afterwards? NOTE: It's a get request site that instantly downloads the file, not a file on a site you can open in your browser EDIT 2: It actually returns xml, not the file, sorry just using a browser downloads it.
Asked
Active
Viewed 143 times
0
-
Possible duplicate of [How to download a file from a URL in C#?](http://stackoverflow.com/questions/307688/how-to-download-a-file-from-a-url-in-c) – theB May 07 '17 at 11:48
-
Nope, not a duplicate. The URL doesn't need the WebClient.DownloadFile, it just needs the GET request. – WebCodingFun May 07 '17 at 11:52
-
@WebCodingFun Whats wrong with my answer? If you like Ill throw in a "how to read the file content back into memory" bit for free. – Sven M. May 07 '17 at 15:45
2 Answers
0
What is the real content of that file?
You can try to configure the content-type as "application/octet-stream". It asks the server for byte content.
If the content is regular text already, you can simply add ".txt" to the file name and you can read it whenever you want.

BezBran
- 161
- 1
- 8
-
The content is XML, but after download it has no extension. It downloads into the download path of your browser. – WebCodingFun May 07 '17 at 14:26
-
0
You do it like this it shouldn't matter if your link has a clear ending like the one I have used. Or if you are really serious about making the GET
part explicit use RestSharp
. Look now you can even change the file extensions from within the code not that it would matter the least bit. I tossed in some Linq2Xml since you mentioned your file was xml and I thought you possible needed to do something with it.
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);
}
}
}

Sven M.
- 509
- 3
- 16