I am trying to write a C# program where I need to download jdk on the machine. I have written some code but it only downloads like 3-4 kb every time
static void Main(string[] args)
{
WebClient client = new WebClient();
string address = "https://download.oracle.com/otn/java/jdk/8u211-b12/478a62b7d4e34b78b671c754eaaf38ab/jdk-8u211-windows-x64.exe";
Uri uri = new Uri(address);
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string fileName = desktop + "\\jdk.exe";
Console.WriteLine("Downloading file");
client.DownloadFile(address, fileName);
Console.WriteLine("Done Downloading File");
Console.ReadLine();
}
If you put the address in a browser, you will quickly see why.
Downloading JDK8 requires you to be logged in now. They do this for older versions of Java. I believe JDK8 started this behavior about a month of so ago.
The URL which you refer redirects to a SSO (Single Sign On) page of oracle site. Probably the 4kb data can be related to the web page and not .exe file. If you plan to automate the download make sure to pass username and password to the SSO page.
Related
I'm currently doing a proof of concept for my application to check for updates and if their is a new version then download that version and install it (prompt the user to install?).
I'm stuck on implementing the download part of the problem, I have an AJAX call with PHP that just returns a file but the type is "application/octet-stream".
<?php #session_start();
$filename = "\\xxx\x\Application\ka-v1.0.0.0.txt";
header("X-Sendfile: $filename");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
?>
The idea is to expand this and check the database what is the latest version and based on that it will form the filename. Originally the plan is to do a scandir on the directory to get all filenames and check for the latest and compare that to the application.
On the Xamarin.Form my code is
_client = new HttpClient();
public async void GetUpdate(string uri)
{
var bytesme = await DownloadFile("https://10.0.2.2:8080/apps/x/ajax/ajax.get_download.php");
File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), bytesme);
}
public static Task<byte[]> DownloadFile(string url)
{
if (!url.Trim().StartsWith("https", StringComparison.OrdinalIgnoreCase))
throw new Exception("iOS and Android Require Https");
return _client.GetByteArrayAsync(url);
}
So the idea is that when the user starts the application then it will call GetUpdates. For now GetUpdate is tied to a button click event. So the idea on the code is that it will download the file via HTTPClient then write it on a Downloads folder.
In this state that code doesn't work
The next step that I haven't research is automatically opening the file to prompt the user to install the new update
Since doing byte download with content-type 'application/octet-stream' is unreliable on Xamarin.Forms I came up with an alternative way of downloading and installing a apk file.
Instead of capturing the data, storing it locally then running it afterwards, I changed that sequence to the API call to a default browser
try
{
await Browser.OpenAsync("https://10.0.2.2/apps/x/ajax/ajax.get_download.php", BrowserLaunchMode.SystemPreferred);
}
catch (Exception ex)
{
// An unexpected error occured. No browser may be installed on the device.
}
So the idea is that the application will get downloaded in the browser and will give the user to open it and install the update.
I'm trying to download files using a list of urls. how would i go about downloading the files if your urls only end in the page where you would normally click the download button(it has a redirect and wait on the download also)?
i havent actually managed to get further than downloading a file using
but i know this wont work because i wont be able to know the filename and ill have to save it to a file in a config location
using (WebClient client = new WebClient())
{
client.DownloadFile("https://github.com/Hellzbellz123/downloadme/raw/master/TestAddon.7z", "testAddon.7z");
}
I intend to build a backend then plug it into a windows forms app for a gui because im really new to C# and programming in general
Do you mean that you don't know the filenames so you don't know how to save them locally?
If so:
//with 'url' as string
WebClient client = new WebClient();
Uri uri = new Uri(url);
client.DownloadFile(uri, uri.Segments.Last());
It takes the URL and splits it by every slash - the last item in the list is the filename..
EDIT: Improved, thanks to Jimi
That method won't work for links like "[..]/download.php?fileid="
For those links take a look at this
I am trying to download file from Google Drive using ASPSnippets.GoogleAP.dll and Google.Apis.Drive.v3.dll. But facing some challenges. File is being downloaded, but it is in some type of weird HTML content.
I am using following code to download it :
GoogleConnect.ClientId = "xxxx.apps.googleusercontent.com";
GoogleConnect.ClientSecret = "xxxxxx";
GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
GoogleConnect.API = EnumAPI.Drive;
if (!string.IsNullOrEmpty(Request.QueryString["code"]))
{
string code = Request.QueryString["code"];
string json = GoogleConnect.Fetch("me", code);
GoogleDriveFiles files = new JavaScriptSerializer().Deserialize<GoogleDriveFiles>(json);
//Remove the deleted files.
var driveService = new DriveService();
Label1.Text = theHiddenField1.Value;
WebClient wb = new WebClient();
wb.DownloadFile(theHiddenField1.Value, "C://" + fileName);
}
else if (Request.QueryString["error"] == "access_denied")
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
}
else
{
GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.readonly");
}
Can someone please help me to resolve the issue?
I'm not a C# developer, but if you're using Google.Apis.Drive.v3, then that is the latest version of the Google Drive API. I don't see any part of your code that references the Google APIs Client Library for .NET, but that's the recommended way to talk to modern Google APIs.
If you've installed it, take a look at the C# quickstart sample for the Drive API. Then check out the C#/.NET examples on the Download Files page in the docs, and it should lead you to a working solution. In your other comment, you asked about passing auth tokens, so if you're using the client library, you shouldn't have to worry about that part. On that docs page, you'll find a sample for regular file downloads and another for exporting Google documents (Docs, Sheets, Slides).
Finally, for additional reference, here are the .NET reference docs for the Drive API and the .NET Google APIs Client Library developers guide.
I am trying to setup a filezilla server. I have followed the instructions from here. I have write a simple c# script in order to upload data to server. My code is the following:
static void UploadFile(string filepath)
{
string m_FtpHost = "ftp://ip:port/";
string m_FtpUsername = "user";
string m_FtpPassword = "pass";
// Get an instance of WebClient
WebClient client = new System.Net.WebClient();
// parse the ftp host and file into a uri path for the upload
Uri uri = new Uri(m_FtpHost + new FileInfo(filepath).Name);
// set the username and password for the FTP server
client.Credentials = new System.Net.NetworkCredential(m_FtpUsername, m_FtpPassword);
// upload the file asynchronously, non-blocking.
client.UploadFileAsync(uri, "STOR", filepath);
}
When I am running that script from the same computer everything is working fine. When I trid to run the same script from another computer of the same lan I got issues. The file doesnt sent it properly. When I turn off the firewall however the upload is happerning normally. Any idea how to get over the firewall?
Usually the Windows asks the user to give permission to a program when it tries to use any port (the Windows get's a pop out asking to allow or disallow the program from using the port ) ...
I'm not sure how to do it but I found a link ...
I'm not sure what conditions need to be met to expose this dialog, I
would assume an application that attempts to open a listening port on
a vanilla Windows instance should always display this dialog. Why
don't you try adding your application to the 'authorized applications'
list, or opening the port manually using the Windows Firewall COM
interop (NetFwTypeLib)?
http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx
quoted from alexw
I have an app with which at startup it downloads a file from a remote location (through the net) and parses it's contents.
I am trying to speed up the process of startup as the bigger the file gets the slower the app starts.
As a way to speed up the process I thought of getting the last modified date of the file and if it is newer from the file on the user's pc then and only then download it.
I have found many ways to do it online but none of them are in C# (for windows store apps). Does anybody here know of a way of doing this without the need to download the file? If I am to download the file then the process is sped up at all.
My C# code for downloading the file currently is this
const string fileLocation = "link to dropbox";
var uri = new Uri(fileLocation);
var downloader = new BackgroundDownloader();
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("feedlist.txt",CreationCollisionOption.ReplaceExisting);
DownloadOperation download = downloader.CreateDownload(uri, file);
await download.StartAsync();
If it helps the file is stored in dropbox but if any of you guys have a suggestion for another free file hosting service I am open to suggestions
Generally, you can check the file time by sending HEAD request and parsing/looking HTTP header response for a Last-Modified filed. The remote server should support it and DropBox does not support this feature for direct links (only via API). But DropBox have another feature, the headers have the etag field. You should store it and check in the next request. If it changed - the file has been changed too. You can use this tool to check the remote file headers.