|
kyrathaba
|
 |
« on: May 13, 2011, 09:09:36 PM » |
|
I have no problem doing this via Filezilla, but I need to figure out how to upload a file to my little corner of DC programmatically, from within my C# applications. I've tried various things, from FTP classes I've both found and rolled myself, and using HttpWebRequest. I also read about the winhttp.dll library, which some people say works whereas the internal C# stuff is not always robust (however, I have no experience with that library's functions). The reason I need to be able to do this programmatically is that I've coded a game. That game records the highscore, and updates that value whenever a player beats the previous high score. I want the program to be able to upload the highscore.dat file (which contains not only the highscore, but also the name of the player who scored it) to a specific directory under http://kyrathaba.dcmembers.comDoes anyone have any working C# code that they've used successfully to upload a file to their DC webspace? I know that DC uses explict FTP over TLS. Perhaps the methods I've attempted aren't sophisticated enough to negotiate that. At any rate, it'd be a real boon (not just to my current project, but also to future ones) if I could find a class or component that allows me this functionality. I have used the code show here before successfully, on a non FTPES site, but can't get it to work here.
|
|
|
|
« Last Edit: May 13, 2011, 09:24:59 PM by kyrathaba »
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
|
|
kyrathaba
|
 |
« Reply #2 on: May 13, 2011, 09:40:22 PM » |
|
Thanks for the possible help. Looks like the project hasn't been updated since Feb. 2008, and that was has been done is mostly focused on SMTP/POP. The only FTP related stuff I could find, so far, was: Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Indy.Sockets.Web { class FTP { } }
I'll keep looking through the download and documentation, though, just to be thorough.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
|
kyrathaba
|
 |
« Reply #3 on: May 13, 2011, 09:54:45 PM » |
|
I tried the following code in a console program, and received this error message: The remote server returned an error: (530) Not logged in. Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] static void Main(string[] args) { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://kyrathaba.dcmembers.com/testbed/test.txt"); request.Method = WebRequestMethods.Ftp.UploadFile; request .Credentials = new NetworkCredential ("kyrathaba", "substitutedForActualPasswordInThisForumPost"); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader ("test.txt"); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); Console.WriteLine("Press a key to end program..."); Console.ReadKey(); }
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
hamradio
|
 |
« Reply #4 on: May 13, 2011, 10:50:47 PM » |
|
Only concern I got is how are you going to keep people from using a .net disassembler to see the password?
|
|
|
|
|
Logged
|
|
|
|
|
mouser
|
 |
« Reply #5 on: May 13, 2011, 11:46:15 PM » |
|
To follow up on ham's point -- you'd be insane to try to save high scores by embedding ftp login credentials in your public app and using ftp protocol to upload user high scores. That's just not the way it's done.
If you want to save high scores the solution is to write a server based script (php, python, perl, dot net on the server, whatever), that responds to http queries from client application over the web, like any other web-based application, and updates the server database, etc.
|
|
|
|
|
Logged
|
|
|
|
|
kyrathaba
|
 |
« Reply #6 on: May 14, 2011, 07:44:36 AM » |
|
I don't know how to write server-side scripts. I guess I could just put up a submission form on a webpage and then have the program direct the user to that form to submit a file via by posting.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #7 on: May 14, 2011, 07:45:31 AM » |
|
Or, the program could invoke the user's default email client to have them send their obfuscated highscore file to me.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #8 on: May 14, 2011, 07:47:08 AM » |
|
Only concern I got is how are you going to keep people from using a .net disassembler to see the password?
To follow up on ham's point -- you'd be insane to try to save high scores by embedding ftp login credentials in your public app
Sorry guys, didn't realize. That's one thing I value most highly about DC; gaining the benefit of others' expertise.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
wraith808
|
 |
« Reply #9 on: May 14, 2011, 09:40:53 AM » |
|
You could also use encryption, and get it from a file if you want to use FTP. Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] using System; using System.Security.Cryptography; using System.Text; namespace Coderz808 { public static class EnDeCrypter { // Set this to some personalized crypto key private const string cryptoKey = "someSortOfCryptoKey"; // The Initialization Vector for the DES encryption routine (choose 8 personalized bytes) private static readonly byte[] IV = new byte[8] { 140, 103, 145, 2, 10, 176, 173, 9 }; /// <summary> /// Encrypts provided string parameter /// </summary> internal static string Encrypt(string s) { if (s == null || s.Length == 0) return string.Empty; string result = string.Empty; try { byte[] buffer = Encoding.ASCII.GetBytes(s); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider (); MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider (); des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey)); des.IV = IV; result = Convert.ToBase64String( des.CreateEncryptor().TransformFinalBlock( buffer, 0, buffer.Length)); } catch { throw; } return result; } /// <summary> /// Decrypts provided string parameter /// </summary> internal static string Decrypt(string s) { if (s == null || s.Length == 0) return string.Empty; string result = string.Empty; try { byte[] buffer = Convert.FromBase64String(s); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider (); MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider (); des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey)); des.IV = IV; result = Encoding.ASCII.GetString( des.CreateDecryptor().TransformFinalBlock( buffer, 0, buffer.Length)); } catch { throw; } return result; } } }
|
|
|
|
|
Logged
|
|
|
|
|
kyrathaba
|
 |
« Reply #10 on: May 14, 2011, 09:42:46 AM » |
|
Thanks, wraith. Yes, and I have the know-how to encrypt a password in my app. I wouldn't put it in raw. But I understand that it's better, probably to use some other file-submission method.
Let me ask this: does DC have a server-side script I could reference from the html code in a webpage, so that a user could upload a small (few bytes) binary file to a designated /incoming/ directory of my webspace?
|
|
|
|
« Last Edit: May 14, 2011, 09:48:30 AM by kyrathaba »
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
worstje
|
 |
« Reply #11 on: May 14, 2011, 10:16:34 AM » |
|
I have an ugly C# function I cobbled together for JottiQ that supports both GET and POST methods, the latter of which supports filetransfers. The code is butt ugly, but it is functional. It returns a XMLDocument if I recall properly, but could be simplified to just return a stream I believe. Let me know if it would be good enough for your needs, and I'll supply you after my shopping run has provided me with foodstuffs to last the weekend. It's probably an excellent example of how not to deal with webservices in .NET, but I don't care much. 
|
|
|
|
|
Logged
|
|
|
|
|
Renegade
|
 |
« Reply #12 on: May 14, 2011, 10:36:28 AM » |
|
I have an ugly C# function I cobbled together... It's probably an excellent example of how not to deal with webservices in .NET, but I don't care much.  Hip! Hip! Horray~! Let's hear it for getting things done~!  I have so much code that's ugly, but works... I wouldn't show it to anyone. Sure, I could clean it up and make it pretty and all that, but... Time... +1 for getting things done. 
|
|
|
|
|
Logged
|
|
|
|
|
worstje
|
 |
« Reply #13 on: May 14, 2011, 10:52:37 AM » |
|
Good point. I hereby rescind my offer to share my code.  The internet has far too long a memory for me to want to carry that blemish for the rest of my life.  Just kidding, I don't give a damn. Just let me know.  Edit: On second thought, I re-read his request, and I think I've got the opposite. I have got client-side C# code that can upload to some http:// script, while I now think he wants a server-side script to receive file uploads and store them with. And with that, I am afraid I cannot help.
|
|
|
|
« Last Edit: May 14, 2011, 11:04:10 AM by worstje »
|
Logged
|
|
|
|
|
mouser
|
 |
« Reply #14 on: May 14, 2011, 11:36:09 AM » |
|
I don't know how to write server-side scripts. kyrathaba, you're a fast learner and, while i'm not suggesting you learn how to write server scripts for this project, i would suggest that you might find it very enjoyable to learn a little bit of php, just as a change of pace from desktop application coding. it would also give you a feel for what kinds of things you can do with server side coding. why not get yourself a book and start the php programming school assignments here.
|
|
|
|
|
Logged
|
|
|
|
|
kyrathaba
|
 |
« Reply #15 on: May 14, 2011, 12:41:55 PM » |
|
Mouser, I promise to look into PHP. In the meantime, worstje, I'd be very grateful for your ugly code  I promise never to share/re-post it anywhere.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #16 on: May 14, 2011, 12:45:50 PM » |
|
Here's my application in its current incarnation ( screenshot). I want to add a menu item "Share my highscore", or something to that effect. It should allow the user to upload the small (20-50 byte) file containing their highscore to a designated /incoming/ directory on DC. I would have another utility program that allows me to scan that particular online folder and download all such files currently residing their, opening each, retrieving username and highscore, and compiling an HTML file showing a listing of the game's users and their highscores in descending order. Would something like this (modified slightly) work? I realize there has be a complimentary PHP script residing on the server... <form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" /> </form>

|
|
|
|
« Last Edit: May 14, 2011, 02:08:52 PM by kyrathaba »
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
mouser
|
 |
« Reply #17 on: May 14, 2011, 01:59:27 PM » |
|
It should allow the user to upload the small (20-50 byte) file containing their highscore to a designated /incoming/ directory on DC. I would have another utility program that allows me to scan that particular online folder and download all such files currently residing their, opening each, retrieving username and highscore, and compiling an HTML file showing a listing of the game's users and their highscores in descending order.  think about what you are saying. You want client to be able to "upload a 20-50 byte FILE" and then you want them to be able to view other people's high scores by scanning a server folder, downloading all other highscore files, opening each, reading info, and then " compiling an HTML file" on the local pc and displaying it. You need to shift your thinking. You do not want this. What you want is something 1000x simpler and more straightforward. You want a server script that will accept two very simple http requests, the first will be submission of high score info, it could be as simple as accepting an http request of http:/kyrathaba.com/highscoresubmit?name=mouser&score=54 or you could use a post form method. The server script will then simply ADD the users score to its high score database. And then to VIEW high scores from the desktop application, you will simply make another web query to the server script of the form http://kyrathaba.com/highscores.php which will RETURN AN HTML FILE that the client can display to view high scores. Users could even just view that web page directly. (Or alternatively return a response with some raw data from the database for you to format and display). You need to stop thinking about doing this purely from the desktop application using protocols like ftp. That way lies madness.
You need to wrap your head around web coding so that you have a better taste for what things you should be doing server-side vs. desktop client side. Go read a few pages about web coding (php, perl, whatever) and using database stuff on the server, etc. You will find it is both easy, fun, and liberating.
|
|
|
|
« Last Edit: May 14, 2011, 02:03:03 PM by mouser »
|
Logged
|
|
|
|
|
kyrathaba
|
 |
« Reply #18 on: May 14, 2011, 02:05:45 PM » |
|
Yeah, I'm very desktopApp-centric in my thinking, because that's all I've every done. It would be better if the server did it all. Is there some sort of free desktop app that I could use in conjunction with developing such a script, just to test the script and know that it will work once uploaded to a server? Or can I just tinker with scripts, uploading them to one of my subdirectories and testing them directly?
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
worstje
|
 |
« Reply #19 on: May 14, 2011, 02:10:37 PM » |
|
You can install a local webserver to develop on, or you can upload each time. For as far software goes, usually a simple webbrowser will do miracles (with or without a temporary html page that has a form that allows you to create the proper sort of request). Either way, I am fully in agreement with mouser. You'll want some scripts on the webserver. Nothing more, nothing less. Forget all that crap about uploading files, scanning directories, etc etc. 
|
|
|
|
|
Logged
|
|
|
|
|
mouser
|
 |
« Reply #20 on: May 14, 2011, 02:11:39 PM » |
|
Yeah, I'm very desktopApp-centric in my thinking, because that's all I've every done. It would be better if the server did it all. Is there some sort of free desktop app that I could use in conjunction with developing such a script, just to test the script and know that it will work once uploaded to a server?
What you should do is install a " LAMP" bundle on your local pc, and which will let you do and test all your web development initially on your desktop. I don't know which package people currently recommend for windows, but one is XAMPP: http://www.apachefriends.org/en/xampp-windows.html which is probably a good option. The most confusing part is actually getting setup and just getting started. After that you will probably find web coding highly rewarding and invigorating.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
kyrathaba
|
 |
« Reply #22 on: May 14, 2011, 02:18:15 PM » |
|
Okay, I'm convinced. Unfortunately, this is going to mean delaying releasing this app. What I really want to do is spend some time in the next few weeks working through the Stage 2 C# assignments in the Programming School. But now it looks like I'll have to break away long enough to figure out how to do the PHP I need for Highscores submission, collating, and reporting in a webpage. Shouldn't be that hard, once I understand the syntax of PHP. Basically, each 'highscore.dat' file just contains a single line of text, such as these three examples, and the program splits them into 'highscore' and 'userWhoAchievedThisHighscore' using the semicolon character as a delimeter: 10500;mouser 8755;kyrathaba 12200;worstje When it saves a 'highscore' on disk, it encrypts it first, so the user(s) of the game can't peek.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #23 on: May 14, 2011, 02:24:16 PM » |
|
So, how would this work, mouser? I have to get some PHP working on XAMP, then submit it to you for proofing/approval, and then it goes in some special directory on the DC server? Or can the script run within a subidrectory of my own DC webspace?
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
mouser
|
 |
« Reply #24 on: May 14, 2011, 02:27:15 PM » |
|
Or can the script run within a subidrectory of my own DC webspace? You can just upload the php files and access and test them from the web, no special permissions or work needed behind the scenes for php. you are going to be pleasantly surprised at how easy it is to start writing server scripts that do interesting things. The only thing you WILL have to eventually have us do is create the initial database for you on the server. after that you will be able to access it and modify it from your scripts.
|
|
|
|
|
Logged
|
|
|
|
|