Programmatically downloading apk from web using Xamarin - c#

I coded an app and I am trying to download a apk from my webspace now. I googled the whole day now and everything I found was for Java I think, but I need C#.
This is what I found (not only this but it's an example): Downloading APK from server and installing it to device (but it's not working I don't know why).
But I have some problems with the code. I can't use the setRequestMethod or getinputstream etc.
Then I tried out this because it seemed to be much easier:
WebClient webClient = new WebClient();
Uri newUri = new Uri("http://mywebspace.bplaced.net/AppDownload.apk");
webClient.DownloadFile(newUri,Path.Combine(Android.OS.Environment.DirectoryDownloads, "AppDownload.apk"));
This is also not working. I'm getting the message "parts of the path not found" (kinda like this, my Visual Studio isn't in English). Doesn't matter what I enter as my path.

This seems to be a duplicate question, which already has been answered here: How to download a file from a URL in C#?
Nevertheless, I have tested your code and for me it is working. I am able to download a file from an url. So, my guess is, that you have a typo in your URL, a problem with the firewall, etc.
Please copy the URL you have entered in the source code and paste it to a Browser of your choice. And see if the download starts. Just to be sure that the URL is correct.
It would better to provide us with the whole error message.

Related

How to download HTML code from WebView webpage?

I would like to know to know how to download HTML code from a webpage in Xamarin. The webpage is opened on a Android device and requires login, therefore it cannot be downloaded using a url link in WebClient. I am interested if the is a possibilty to download the HTML code from the opened webside, so that it can be used to notify me about changes in the webside.
My code to open the webside (then i need to log in):
void WebViewFunction()
{
webView1 = FindViewById<WebView>(Resource.Id.webView1);
webView1.Settings.JavaScriptEnabled = true;
webView1.SetWebViewClient(new Client());
webView1.LoadUrl($"https://www.strava.cz/Strava/Stravnik/Prihlaseni");
}
I have absolutely no idea how to do it since I am a beginner.
Thanks for your answer.

Send RTSP Authentication C#

I am simply trying write a program in C# (windows forms) to play a video from an acquired URI that is password protected. The point of the program was to locate a URI using SOAPs and other means in order to stream a specific video feed. I didn't know the most difficult part of the project would be simply playing the video. I assumed there had to be libraries out there that could handle this. While there is plenty that can play a video, I am struggling to figure out how to handle RTSP streaming with authentication.
Tried formatting the URI object with UserName and Password. This throws a bad formatting error. Even though the final results follows good formatting according Wiki "IsWellFormedUriString" still throws an error.
local_uri.Host = deviceUri.Host;
local_uri.Port = 554;
local_uri.Scheme = "rtsp";
local_uri.Password = "admin";
local_uri.UserName = "admin";
//List full URI info.
infoBox.Text = local_uri.ToString();
//Past it to VideoView and start playing video.
bool check = Uri.IsWellFormedUriString(local_uri.Uri.ToString(), UriKind.RelativeOrAbsolute);
bool check = true;
if (check)
{
//_mp is of type LibVlcSharp.Shared.MediaPlayer
_mp.Play(new Media(_libVLC, local_uri.Uri));
check = false;
}
else
check = false;
I've tired to get a depreciated version of the VLC library working but seem to be running into the same issues. Looked through c# VLC lib and didn't find any function that would obviously handle authorization. I then started just trying to write code to handle, buffer, and play the data. I think this will be out of the question for how time consuming it is and given the time constrains on this assignment.
I know the URI i get back is valid. I can open VLC and entire the URI. A moment later the following pops up...
Once its entered I see a RTSP package sent to the URL...
DESCRIBE rtsp://192.168.1.64:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1 RTSP/1.0
CSeq: 7
Authorization: Digest username="admin", realm="IP Camera(C6990)", nonce="61218496a6adcf4869f748505626d63a", uri="rtsp://192.168.1.64:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1", response="3cce04175b5b1a3d0a6a8dcfea9d377a"
User-Agent: LibVLC/3.0.11 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
Then of course the URL responds with an 200 Ok and starts streaming. It would be great if I could figure out how to recreate this event in C#. I had a recommendations of doing a Dialog API but cannot figure out how to work that out with the VLC Object. Any direction would be greatly appreciated. Hindsight I should not have chosen a different project as this is attached to my grade.
As it turns out (and I'm not entirely sure why) but the code above does work as long as your password does not contain anything that would cause string formatting error. For example, the password cannot contain an "#" symbol with the code above. I'm sure there is a workaround for this but have not figured out what it is.

Automatically uploading a file to Google Drive File Stream with a Windows Service

I have made a Windows service that;
Downloads a CSV file from a regularly updated internal database through a WebClient every hour.
Put that CSV file into a designated folder.
In the original test case, it was put into a local folder on my desktop (C:).
The test case worked perfectly.
The CSV would replace the old file with the newly downloaded one with the same name.
As listed above. This works perfectly on a local folder. However, we intend for it to work through Google Drive File Stream. As we have a Google Sheet that manipulates and sought the data for us for any CSV file that is under the given name.
This is the current method of downloading and placing the file.
public void CSVDownload()
{
string url = #"YOUR_CSV_URL_HERE";
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri(url), #"YOUR_GOOGLE_DRIVE_FILE_STREAM_FOLDER_HERE\NAME.csv");
void client_DownloadFileCompleted(object CSVDownload, AsyncCompletedEventArgs e)
{
eventLog1.WriteEntry("CSV File (NAME.csv) downloaded");
}
}
My question is why can't I currently automatically upload a file to Google Drive File Stream through a Windows Service? Is it because Google Drive File Stream requires certain permissions or actions? Is it because the drive is "virtual" and not physical (H:)? Below is the folder I am trying to upload to in Google Drive File Stream (H:\My Drive\Test).
The code also runs through completely as the log files show that the methods are used. However, there seems to be some block between the download and placing of the CSV file in a Google Drive File Stream folder.
Update: There has been little progress so far. One of my colleagues maybe suggests that there needs to be some sought of user permission to push. Like a username and password. If this is true does anyone know how I could achieve this?
Update #2: In the 'Registry Editor' I found some interesting info.
Press Windows + R
Type regedit.exe
HKEY_CURRENT_USER -> SOFTWARE -> Google -> DriveFS -> Share
As you can see there are two values. 'MountPoint = H' is obviously the drive letter it is mapped to and 'ShellIpcPath = \\.\Pipe\GoogleDriveFSPipe_user.name_shell' which might be useful. I played around with ShellcpPath but to no prevail.
Update #3: #Ben Voigt mentioned to use DriveInfo.GetDrives() to see if the drive is found by the service. I run the code and it looks like it does exist.
Here is what the console spit out:
Drive H:\
Drive type: Fixed
Volume label: Google Drive File Stream
File system: FAT32
Available space to current user: 15987068928 bytes
Total available space: 15987068928 bytes
Total size of drive: 16106127360 bytes
As you can see it exists however it uses File system: FAT32 instead of File system: NTFS which all my other drives use (C:),(D:),etc. So it seems that only Google Drive File Stream uses FAT32.
Test Case:
Works perfectly when placing the CSV files into a local folder.
client.DownloadFileAsync(new Uri(url), #"C:\Users\user.name\Desktop\Test\designtasks.csv");
client.DownloadFileAsync(new Uri(url), #"C:\Users\user.name\Desktop\Test\designjobs.csv");
Does not work when placing into a Google Drive File Stream folder.
client.DownloadFileAsync(new Uri(url), #"H:\My Drive\Test\designtasks.csv");
client.DownloadFileAsync(new Uri(url), #"H:\My Drive\Test\designjobs.csv");
To clarify about the test case. I am actually getting two CSV files. However, the code is basically the same. So I only mention getting one in my original question to keep it cleaner and more straightforward.
I found the solution! It might not be optimal for all cases however it works in my case.
Here is what I did;
Make a project installer with InstallerProjects.vsiz. Obviously, if you are using a different version of VS, this might change. Some of the older version already come with it installed and the newer versions do not automatically include it. You will have to also find the correlating version as the one linked is for VS2017. Here is a tutorial on how to implement an installer on a windows service.
Once you have implemented the installer, right click on serviceProcessInstaller1 (or whatever you decided to call it) and select properties.
In the properties window, look a for the 'Account' field and set it to 'User'.
Save and build you project. That's it.
What does's this actually do? In plain English; It basically makes the service impersonate a user so it can read and write files on the system.
When installing on a different computer from which the service was built on, 'Windows Defender Shield' will block the service from installing. This is just Windows bring cautious. Obviously, if you built the service without ill intent this will not harm your computer.
To continue installation click "More info" on the 'Windows Defender Shield' screen and click "Run anyway".
Before the Service finishes installing, it will stall and ask for user information. In most use cases, giving it your own user information ill suffice.
If you are a user on a domain or anything along those lines. You will need to add the domain prefix to the 'Username' field. Example: DOMAIN\user.name.

Attaching auto generated pdf to email in asp.net app

I have a very specific requirement. In my web app, I have to generate a pdf invoice from the database values, and an email body. I can easily send this using SMTP which works perfect.
But, problem is we can't rely on system to always be perfect, and this is an invoice. So, we need to open the default mail client instead of using SMTP. Right now, I have following code
//Code to create the script for email
string emailJS = "";
emailJS += "window.open('mailto:testmail#gmail.com?body=Test Mail" + "&attachment=" + emailAttachment + "');";
//Register the script for post back
ClientScript.RegisterStartupScript(this.GetType(), "mailTo", emailJS, true);
This opens the email perfectly, but no attachment is working. Path is supposed to be /Web/Temp/123.pdf.
If I use the same path as normal url like below, it opens the file in new window properly.
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", "window.open('/Web/Temp/123.pdf');", true);
So, clearly file exists, but it exists on the server. Outlook on the other hand open on client machine. So, I can't use the full determined path like C:\Web\Temp\123.pdf. If I try that, it will try to find the file on client machine, where the folder itself might not exist.
I am trying to figure out what can I do here. If there is another method I should try.
P.S. No, I can't send the email directly. That will cause a hell lot more problem in future for me.
Edit:
I also found one weird problem. If I add a double quote to the file path in attachment, a \ is added automatically. #"&attachment=""" + Server.MapPath(emailAttachment) + #"""');" gives me output as &attachment=\"C:\Web\Temp\123.pdf\".
I am trying to escape that double quote and somehow it adds that slash. I know this is a completely different problem, but thought I should mention here, instead of creating a new question.
Edit:
I tried a fixed path on localhost. So, I am basically testing the app on the same machine where file is getting stored. still, no attachment at all.
string emailJS = "";
emailJS += #"window.open('mailto:jitendragarg#gmail.com?body=Test Mail" + emailAttachment + #"&attachment=";
emailJS += #"""D:\Dev\CSMS\CSMSWeb\Temp\635966781817446275.Pdf""');";
//emailJS += Server.MapPath(emailAttachment) + #"');";
//Register the script for post back
ClientScript.RegisterStartupScript(this.GetType(), "mailTo", emailJS, true);
Updated the path to make sure it is proper. Now, it just throws error saying command line argument not valid.
Edit:
Is there any other method I can try? I have the file path on the server side. Maybe I can download the file automatically to some default folder on client machine and open from there? Is that possible?
Edit: I tried one more option.
emailJS += #"mailto:testmail#gmail.com?body=Test Mail" + #"&attachment=";
emailJS += #"\\localhost\CSMSWeb\Temp\635966781817446275.Pdf";
//emailJS += Server.MapPath(emailAttachment) + #"');";
Process.Start(emailJS);
The Process.Start line works but it does nothing at all. There is no process that starts, no error either.
Edit:
yay. I finally got the user to approve using a separate form to display the subject and body, instead of opening the default mail client. although, I would still prefer to solve this problem as is.
So, the problem here is the fact that mailto only supports direct file path for attachment. That is, path has to be local to use machine, or intranet path within the network.
In other words, path like http://yourapp/Web/Temp/123.pdf won't work, and /Web/Temp/123.pdf being essentially the same won't work either. These are not paths, but links to files that has to be downloaded and stored locally before they can be used as attachments - mailto protocol has no support for that.
However, since your application is intranet, what you could do is make sure intended users have access to some network shared folder on your server, and then provide them with network path to the file, that is \\theserver\files\123.pdf

The parameter is incorrect. Server or code issue?

Can anybody tell me why I'm getting the error message, "the parameter is incorrect" on one of the pages in my project. Basically, this is happening only in IE, and only one 1 server. I have a dev, a test and a live server. The link has a querystring value "city=Québec". I'm not sure if it has anything to do with the special french e character, but this code works in all browsers on the dev and test sites, and in chrome and firefox on the live server, but if I use IE on the live server, the link sends me to a completely white page that says "the parameter is incorrect". I have absolutely no idea why. Anyone have any idea?
My guess is that Firefox is performing UTF-8 encoding on the URL automatically and IE is not. Just change the link to a UTF-8 encoded version and you should be fine. Example: city=Qu%C3%A9bec
It wouldn't hurt to double-check the HTTP headers your server is sending ....
... but clearly the main problem is in the browser and/or client PC.
Look at these links:
Browser language settings (old version of IE):
http://support.microsoft.com/kb/185035
Unable to create temp files:
http://www.ehow.com/info_12218814_temporary-internet-files-parameter-incorrect.html
Q: What version of IE is experiencing the problem?

Categories