C# upload file to server - both client and server side - c#

I'm a C# game programmer with little web development experience.
I need to upload a small file (25-100 or so bytes, depending on it's content) to a server. This is on the Windows Phone 7 using XNA. The target server is fairly limited and only supports PHP and classic ASP.
Since the CF on the WP7 only has access to a limited subset of networking commands, it's looking like an HttpWebRequest GET aimed at a script that saves the file will be the best option. The data I'm sending is small in size, and should be able to be passed as a parameter in the url.
I've been searching but have yet to find a complete example of this, which handles both the client and server side script (mainly the latter). This is close to what I'm looking for, except it has no mention of the server side script: Upload files with HTTPWebrequest (multipart/form-data)
The closest that I got was this: http://www.johny.org/2007/08/upload-using-c-as-client-and-php-as-server/
But when attempting to use it I get an unhandled exception: "The remote server returned an error: (405) Method Not Allowed". This method seems the most promising so far, but I've yet to be able to debug this.
Unfortunately, I have a short amount of time to implement this, and as I said only a passing familiarity with web development. I'm not worried about maximum security or scalability as this is a temporary measure to collect feedback internally. Basically, I just need the quickest thing that works. ;)
Any help would be fantastic!

I've solved it. First off, PHP wasn't supported on my server (just now learning that PHP and ASP are can't be used on the same server, depending on whether it's on Linux or Windows - like I said, web development noob here!). I switched to ASP and, after digging through the docs, wrote this script:
<%
dim theData, theFileName
set theData=Request("data")
set theFileName=Request("filename")
dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(Server.MapPath(theFileName+".txt"))
tfile.WriteLine(theData)
tfile.Close
set fname=nothing
set fs=nothing
set theData=nothing
set theFileName=nothing
%>
This C# code uploads the file:
const string cAddress = "http://site.com/folder/upload.asp";
string fileName = foo;
string data = bar;
string address = cAddress + "?filename=" + fileName + "&data=" + data;
uploadRequest = (HttpWebRequest) HttpWebRequest.Create(address);
uploadRequest.Method = "GET";
uploadRequest.GetResponse();
Hope this helps someone else looking for an example of how to do this!

But you have the METHOD as GET instead of POST. You can't upload a file to a website by passing the file path to the Query String.

Related

Is it possible to use NSUrlSession to upload files to an FTP server?

I've been scouring the web for a few hours looking for a solution to a problem that I need to resolve with a Xamarin.iOS app that I've been developing. Most, if not all, of our customers in the field need to upload pictures that they attach to work orders (tickets), which go to an FTP on our backend. My coworker developed the Android app and from him I took the current method, based off of an FtpRequest casted to an FtpWebRequest. It works 90% of the time and uploads to the server reasonably quick if the user has a decent connection.
The problem? I need 100% of the time, and I need it to be in the background. I found that NSUrlSession seems to be my best bet for solving this issue, as it's native to iOS and has support for backgrounding. My only issue is that when I try to create the UploadTask with session.CreateUploadTask, it says "cannot read file at (path)" where path is: file://var/mobile/Containers/Data/Application/850CB1FE-9C2D-456C-8B5F-921DC8D5CEF5/Documents/PolarisPictures2/VUPSXOUTA722002799CMC5022017103109544681088_1.jpeg. I've already confirmed that the file does exist on that path via printing out the file name using foreach( var file in Directory.EnumerateFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PolarisPictures2") ) Without using the file:// protocol, the error returns with "...is not a valid file:// url." So is it that I'm not accessing the file properly, or is it that there's simply no way to do what I'm trying to do?
And if there is no way, could anyone provide a solution that would best achieve the backgrounding capabilities that I require?
Appreciate it, guys.
Thanks.
Here's my code:
NSUrlSession session = null;
NSUrlSessionConfiguration config = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration("com.PolarisFTPUpload.BackgroundSession");
session = NSUrlSession.FromConfiguration(config, (NSUrlSessionDelegate)new MySessionDelegate(), new NSOperationQueue());
string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
docs = docs.Replace("/var", "file://var");
string filepath = System.IO.Path.Combine(docs, "PolarisPictures2/VUPSXOUTA722002799CMC5022017103109544681088_1.jpeg");
Console.WriteLine(filepath);
string UploadURLString = "ftp://myftpUser:myftpPass#myftpIP:Port/Pictures/Database" + "/" + DateTime.Today.Year + "/" + DateTime.Today.Month + "/" + DateTime.Today.Day + "/";
NSUrlSessionUploadTask uploadTask;
NSUrl uploadURL = NSUrl.FromString(UploadURLString);
NSUrlRequest req = NSUrlRequest.FromUrl(uploadURL);
uploadTask = session.CreateUploadTask(req, NSUrl.FromString(filepath));
Neither NSURLSession nor its predecessor, NSURLConnection, support the FTP PUT command. The only Apple API that supports FTP uploads is CFFTPStream, which is ancient, deprecated, and strongly discouraged for new development.
Either way, you should not under any circumstances use FTP. Period. It is fundamentally impossible to make FTP secure, because passwords are sent in cleartext across the network.
Instead, you should write a simple bit of PHP code on your web server that accepts file uploads, and use HTTPS uploads. Not only is that approach more secure, it is also supported with NSURLSession.
See Secure PHP File Upload Script for more information on the web site of things, and see Apple's NSURLSession Programming Guide for help with the uploads on the iOS side.
There should be three / with file prefix. The prefix should be file:///, not file://.
You can just use NSUrl.FromFilename(filepath) to get this url without replacing the prefix.

C# Web Service (404) Not Found.

This code used to work and now it stopped working :/
I'm using the same code in other areas of my application and can call the link which works fine. But in this specific section of my application, it stopped working.
This link is valid and as stated above, it used to work.
Here is the code.
WebClient client = new WebClient();
string xmlResult = client.DownloadString(_workContext.AccountingWebServiceLink + "?action=updateprimary&xml=" + strXML);
I'm not sure if I need to increase the size of bytes in IIS? I suspect it might have something to do with the amount of data being sent.

C# Download Data using Web Client

I'm having an issue downloading a file. I'm running this website on my local IIS. The BaseUrl correctly has the address of my local IIS site. The moduleImgPath is a Sitecore media item: "/sitecore/shell/~/media/Racking/module-image.png". The BaseUrl has the structure "http://local-$company.com".
The code used for the download is essentiall shown below. The method errors on Image.FromStream() with a System.ArguementException - "Parameter is not valid."
Stream stream = new MemoryStream(new WebClient().DownloadData(RackingConfigHelper.BaseUrl + moduleImgPath));
Image objImage = Image.FromStream(stream);
My question essentially revolves around - can you use a WebClient this way, to download data from what essentially a local source? Or will I need to deploy this code to my test environment to test it out? If I can, do I need to worry about ports?
It looks like your image data is not a jpg (I think this is typically what is expected for Image).
You can see why the error happens on this msdn link
and you can see the docs for Image on this msdn link
I would hazard a guess you are either trying to use a non-jpg OR perhaps the jpg you have may be unusually formed.

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.

Get original url without non-standard port (C#)

First question!
Environment
MVC, C#, AppHarbor.
Problem
I am calling an openid provider, and generating an absolute callback url based on the domain.
On my local machine, this works fine if I hit http://localhost:12345/login
Request.Url; //gives me `http://localhost:12345/callback`
However, on AppHarbor where I'm deploying, because they are using non-standard ports, even if I'm hitting it at "http://sub.example.com/login"
Request.Url; //gives me http://sub.example.com:15232/callback
And this screws up my callback, because the port number wasn't in the original source url!
I've tried
Request.Url
Request.Url.OriginalString
Request.RawUrl
All gives me "http://sub.example.com:15232/callback".
Also to clear up that this isn't a Realm issue, the error message I am getting from DotNetOpenAuth is
'http://sub.example.com:14107/accounts/openidcallback' not under realm 'http://*.example.com/'.
I don't think I've stuffed that up?
Now, I'm about to consider some hacky stuff like
preprocessor commands (#IF DEBUG THEN PUT PORT)
string replace (Request.URL.Contains("localhost"))
All of these are not 100% solutions, but I'm sick of mulling over what could be a simple property that I am missing. I have also read this but that doesn't seem to have an accepted answer (and is more about the path rather than the authority). So I'm putting it towards you guys.
Summary
So if I had http://localhost:12345/login, I need to get http://localhost:12345/callback from the Request context.
And if I had "http://sub.example.com/login", I should get "http://sub.example.com/callback", regardless of what port it is on.
Thanks! (Sleep time, will answer any questions in the morning)
This is a common problem in load balanced setups like AppHarbor's - we've provided an example workaround.
Update: A more desirable solution for many ASP.NET applications may be to set the aspnet:UseHostHeaderForRequestUrl appSetting to true. We (AppHarbor) have seen several customers experience issues using it with their WCF apps, which is why we haven't enabled it by default and stil recommend the above solution for those situations. You can configure it using AppHarbor's "Configuration Variables" to inject the appsettings when deployed. More information can be found in this article.
I recently ran into an issue where I compared a URL to the current URL, and then highlighted navigation based on that. It worked locally, but not in production.
I had http://example.com/path/to/file.aspx as my file, but when viewing that file and running Request.Url.ToString() it produced https://example.com:81/path/to/file.aspx in a load balanced production environment.
Now I am using Request.Url.AbsolutePath to just give me /path/to/file.aspx, thus ignoring the schema, hostname, and port numbers.
When I need to compare it to the URL on each navigation item I used:
New Uri(theLink.Href).AbsolutePath
My initial thoughts are get the referrer variable and check if that includes a port, if so use it otherwise don't.
If that’s not an option because a proxy might remove the referrer header variable then you might need to use some client side script to get the location and pass it back to the server.
I'm guessing that AppHarbor use port forwarding to the IIS server so even though publicly the site is on port 80 IIS has it hosted on another port so it can't know what port the client connected on.
Something like
String port = Request.ServerVariables["SERVER_PORT"] == "80" ? "" : ":" + Request.ServerVariables["SERVER_PORT"];
String virtualRoot = Url.Content("~/");
destinationUrl = String.Format("http://{0}{1}{2}", Request.ServerVariables["SERVER_NAME"], port + virtualRoot, "/callback");
If you use the UrlBuilder class in the framework you can easly get around this. On the builder class if you set the port to -1 then the port number will be removed:
new UriBuilder("http://sub.example.com:15232/callback"){ Port = -1}
returns : http://sub.example.com/callback
To keep the port number on a local machine just check Request.IsLocal and don't apply -1 to the port.
I would wrap this into a extension method to keep it clean.
I see that this is an old thread. I had this issue running MVC5, on IIS 7.5, with an Apache proxy in front. Outside of the server, I would get "Empty Response", since the asp.net app gets the Url from apache with the custom port.
In order to have the app redirect to a subpath without including the "custom" port, forget the Response/Request objects, and use the Transfer method. For instance, if I want that users are automatically redirected to the login page in case they are not logged already:
if (!User.Identity.IsAuthenticated)
Server.TransferRequest("Account/Login");

Categories

Resources