Download zipball from github in C# - 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.

Related

Blazor upload file issue

I'm writing a blazor server app that will sit in the adobe marketplace
I want to upload a file through the app and place it on a server
When I call the standard code for uploading a file , I get the following exception
'''
FileStream fs = File.Create(path);
await file.OpenReadStream().CopyToAsync(fs);
fs.Close();
'''
"An error occurred while reading the remote stream: TypeError: r.arrayBuffer is not a function"
I iBrowserFile onjetc has data and I can create a buffer with the correct bytes
I can create a placeholder on the server but cannot add the stream to the path Ive created
if I run the app outside of adobe , it uploads just fine
I cant find anything related to this error , is there any other way to upload a file ?
This may not fully answer your question, but a related GitHub issue indicates a low RAM issue. In my case, this issue was present on older iPads but newer iPads did not have this issue. As this happens over SignalR, this is most likely lack of browser support for handling the file.
My only solution I could find was to not support the old version of iOS that is causing this.

unable to delete directory from SFTP server

I am trying to delete the directory from my SFTP server. I am using EnterpriseDT.Net.Ftp SSHSCPClient for connecting to the server with private key, no password.
When I use the
SSHSCPClient ftpConnection;
//Successfully connected and able to upload the files but
ftpConnection.MultipleDeleteDirectories(remoteFolder);
it throws the error
[Message = "This operation is not currently supported."]
Just tried SSHSCPClient from v9.1.3.20 assembly, and using dotPeek found out that MultipleDeleteDirectories and all other delete methods are throwing not supported exception deliberately. Screenshot below. Or by any chance they have another privately distributed assembly, which has all the methods implemented...
edtFTPnetPRO looks a little bit outdated. Unless you have a good reason to use this library, I would recommend using SSH.NET which supports .NET Core, and is open source.

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

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

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.

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