WebClient.DownloadFile issues and Response Header (Vary: negotiate,Pragma: no-cache) - c#

I have a SSIS job where in one of its tasks i use a Script Task (C# code) that tries to download a CSV file (using WebClient.DownloadFile Method) from a given website to be processed by later stages.
The download of the CSV would fail every time so in trying to investigating the issue i came across This Blog and found that when URL of the CSV File has Vary: negotiate and Pragma: no-cache in its Response Header then download fails. IE8 is the latest version of IE available on host server so i don't have a say on that. The following error is displayed when trying to download the CSV file using IE8:
Response Header when trying to download CSV file:
When i run Fiddler with following Filters the download of CSV works as expected in IE8:
So my question is how can i do this with WebClient.DownloadFile ? I looked into Fiddler.Core but couldn't figure out how to do this. I also looked into simply running Fiddler using:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\PROGRA~1\Fiddler2\Fiddler.exe";
But problem with the above method is that, when the Process.Kill() is called Fiddler has no chance to cleanup after itself and leaves behind its proxy ...etc resulting of disruption of connectivity for other applications.
Is anyone able to tell me how i can deal with this situation? Thanks

The .NET WebClient implementation does not suffer from the limitation described in my IEInternals blog post that you cited above. WebClient is based directly on sockets, not WinINET. Your problem has nothing to do with these headers.

Related

C# Cant to download JDK15

I'm trying to download JDK15 but i downloading 5307 bytes but this are not the JDK15
using (WebClient wc = new WebClient())
{
wc.DownloadFileAsync(new System.Uri(
"https://download.oracle.com/otn-pub/java/jdk/15.0.1%2B9/51f4f36ad4ef43e39d0dfdbaf6549e32/jdk-15.0.1_windows-x64_bin.exe"),
Path.Combine(directoryPackagesPath, "jdk.exe")
);
}
You downloaded a web page instructing you how to access the download:
You cannot directly download that file. If you open it in another browser, or a incognito/private window, you will see this message:
Sorry!
In order to download products from Oracle Technology Network you must
agree to the OTN license terms.
If you open the downloaded file, most likely it's an HTML with this message.
It is illegal, but if you want learn how to do it for some CTF etc.:
Get query what is send after accepting licence by dev tools in your browser.
Send the exact same query by HttpClient and get cookies from response.
Use this cookies to get the file.
If you want this particular version, you can attach it (if licence allows you to do that) to your program by using installer, by resources or even as normal file in output directory.

Download zipball from github in C#

Need to download a zipball from given link in C#
YES! I searched the net and stackoverflow and been trying to accomplish this seemingly impossible task for hours...
Ironically, in Curl it's single line and works like charm..
curl -L https://api.github.com/repos/username/reponame/zipball > repo.zip
I want to do in C# same thing curl does above...
Tried WebClient.DownloadFile()
Gives
forbidden (403)
Tried async method too
Gives
0 bye file (no error/exception)
Tried HttpClient Datadownload and File stream writer, give same errors as above. Seams like streamwirter is not invoked at all so it's failing to access the server that's the main part of my problem
I have Octokit.NET installed but it lacks documentation so I'm not even sure where to start doing this (probably it's WebClient version but I did try that in .NET libs)
Found this answer but didn't really understand it (Cannot get repository contents as .zip file (zipball) in Octokit.net)
Even tried to run .sh script in C# but it gives me exception that it can't run this kind of shell on this OS
When I tried this with WebClient, I didn't get a 403, but an exception:
System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine
Looking up other questions with the same error, I found that the GitHub API server requires a user agent to be set. After that, it was trivial:
using System;
using System.Net;
class Test
{
static void Main()
{
using (var client = new WebClient())
{
client.Headers.Add("user-agent", "Anything");
client.DownloadFile(
"https://api.github.com/repos/nodatime/nodatime/zipball",
"nodatime.zip");
}
}
}
... that worked fine. I've tried it for a user repo instead of an organization, and that was fine too.
You definitely wouldn't want to do anything with a StreamWriter, as that's for text data - and a zip file isn't text data.
You haven't shown what you did with the async version - my guess is that you started downloading but didn't wait until it had completed before disposing the client.

How can you run a PHP file without opening a web browser from a C# program?

I am writing a tool which will allow users to communicate with each other over the internet using a server and PHP files that I have set up. I have written it, but right now when I open the PHP files and pass arguments through the URL to create new files on my server, it opens the PHP file in my default browser. This is the code I am using right now to open the PHP files on my server:
private void ExecuteProcess(string FilePath)
{
Process Process = new Process();
Process.StartInfo.FileName = #FilePath;
Process.Start();
}
I want to be able to open files in a similar way without physically opening them in my browser. I have been googling around for a few hours, but whenever I try to user the methods that I find on the internet I get a 406 exception from Visual Studio, saying that the server cannot fufill my request? My write permissions are set to read for these files, do I need to change these?
Thanks for helping a PHP noobie,
-I
I think you want to make an HTTP request to your server. Check the WebRequest class.
When i used the web request class, there was a page 406 error, which meant that the servers acceptable headers were not comparable with the type of data I was requesting. By default, mod security is turned on on apache servers, and I just need to disable it to allow me to download data with the web request class.unfortunately, the server is hosted by a third party, so I will have to contact the web master in order to turn this off. I have opted just to host my own server, and avoid this hassle.

Downloading a file in C# incorrectly returns files that is zero bytes long

So I'm trying to Download a file using WebClient class but the problem is that when the download is finished the file that should be downloaded is 0 byte, I tried uploading the same file without extension and than changing it after download but that didn't help. What Can I do? This is the code I Use
WebClient updateDownloader = new WebClient();
updateDownloader.DownloadFile(new Uri("http://zazaia.ucoz.com/SomeExeFile.exe"),
Application.StartupPath + "\\SomeFile.EXE");
And also have DownloadCompleted event handler which just shows MessageBox and Disposes the WebClient.
There is nothing wrong with the code you have shown and this should work. The problem is on the server which is not returning the file properly. Also make sure that the site you are querying doesn't require some authentication before being able to download files. In addition to that don't forget that a WebClient will not execute any javascript, so if the server relies on it to download the file, this will not happen.
Have you checked that your antivirus is not interfering? Sometimes an automatic scan will lock an executable file being downloaded until it passes. The client code itself looks fine however.
What about the server side? If is one of your own applications serving the download, then it may not be setting the MIME header or even not handling the download correctly at all

How do I make WebClient.DownloadFile() work with xnb, xgs, xsb, xwb files?

I'm creating an Updater program in C# for my PC game that basically sends an Http message to the server to find out what the latest version of the game is. If there is a newer version, it downloads the necessary files. To download the files I used the WebClient.DownloadFile() method. There are a few posts on the forums detailing problems with this method but none of them are quite like mine.
I use the method like this:
WebClient webClient = new WebClient();<br/>
webClient.DownloadFile(sOriginFile, sDestinationFile);
I immediately ran into a problem downloading any files with the following extensions:
.xnb
.xgs
.xsb
.xwb
I would get an exception stating "The remote server returned an error: (404) Not Found."
So as an experiment I added 3, more common, files to the same directory.
.txt
.doc
.jpg
and the DownloadFile() method worked perfectly for those files. Does anybody know why this method isn't working for the first 4 files types but works fine with the last 3?
Also I tried WebClient.DownloadData() and HttpWebRequest.GetResponse() (after setting up the request), I even tried reversing the extension name on the server (.bnx), but no matter what, I would get the same exact exception.
If anybody really wants to tackle this, here are links to 2 sample files (I tried to post all 7 sample files but Stack Overflow only allows me to post 2 links):
http://www.facepuncher.com/Versions/CastleAbra/1.1/Sample.txt
http://www.facepuncher.com/Versions/CastleAbra/1.1/UiCursor.xnb
Most likely the MIME-Settings for the file types you mention are set up incorrectly in IIS. Go to IIS Server Mananger -> MIME-Settings and add the file-types accordingly.
Probably a better idea to transfer any filetype would be to download only files like
file.xnb.dat
file.xgs.dat
and rename them locally.
-Matthias

Categories

Resources