How to download a file from a URL using c#
Lasse Vågsæther Karlsen is correct. Your browser is smart enough to decompress the file because the response contains the header:
content-encoding:"gzip"
You can download and decompress the file with this code (adjust accordingly for your file name, path, etc.)
void Main()
{
using (var client = new WebClient())
{
client.Headers.Add("accept", "*/*");
byte[] filedata = client.DownloadData("http://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=1396-08-08");
using (MemoryStream ms = new MemoryStream(filedata))
{
using (FileStream decompressedFileStream = File.Create("c:\\deleteme\\test.xlsx"))
{
using (GZipStream decompressionStream = new GZipStream(ms, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
}
}
}
C# Download file from URL
Looking in Fiddler the request fails if there is not a legitimate U/A string, so:
WebClient wb = new WebClient();
wb.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36");
wb.DownloadFile("http://www.cryptopro.ru/products/cades/plugin/get_2_0/cadeplugin.exe", "c:\\xxx\\xxx.exe");
Download a file from website, without specific file url
Try this approach:
var client = new HttpClient
{
BaseAddress = new Uri("https://thunderstore.io/")
};
var response = await client.GetStreamAsync("package/download/Raus/IonUtility/1.0.1/");
var fn = Path.GetTempFileName();
using (var file = File.OpenWrite(fn))
{
await response.CopyToAsync(file);
}
At the end fn
will hold the local file name. There is no dialog and you have the full control.
Related Topics
Return Json, But It Includes Backward Slashes "\", Which I Don't Want
How to Ignore First Two Columns of CSV File
Linq Multiple Group by in a List<T> Then Convert to List<T>
How to Set a Cookie on Httpclient'S Httprequestmessage
Clearing a Textbox Leaves an Invisible Character
Asp.Net Core Localization Decimal Field Dot and Comma
Fetching Value from a Datatable into Datatable With Where Clause
Count() a Specfic Attribute Within a List C#
Map a Property to List of Object Using Automapper Createmap
How to Call Another Controller Action from a Controller in MVC
Copy Few Items from One List to Another List of Same Type in C#
C# File.Delete, File Being Used by Another Process
How to Get the Current Project Directory from C# Code When Creating a Custom Msbuild Task
Getting the Latest File Modified from Azure Blob
Convert a List of Objects from One Type to Another Using Lambda Expression
Searching If Value Exists in a List of Objects Using Linq
How to Write Data on Multiple Lines But Within the Same Cell of Csv