C# - Send a file over HTTP - c#

In a server / client environment, I need to send a plain text file over the HTTP protocol.
Typical scenario:
A client joins the server, and the server sends a string telling the client a url to download a text file. The URL would be:
"IP:PORT/folder/folder/file.txt" (where 'IP' and 'PORT' are actual
IP's and Ports IE: 127.0.0.1:1234)
I need the server to allow a connection to the files location, and the files location alone.
The client is closed source, and so I have no control over its code. It should act like a HTTP server, but only for that file, or at least the subfolder. You should be able to type in the URL into a browsers address and read the contents of the file.
What's the best way of doing this?

The easiest way would be to use the HttpListener class.
The documentation contains an example which shows you how to set up the listener and receive a request, but it does not directly cover how to check what file was requested and how to feed that file back to the client. However, both are easy to do and would not take a lot of code.

You can try to use an existing webserver like Apache to serve those files.
Also if this is some kind of learning exercise. You can implement your own simple HTTP server in C#

Related

Send and receive a file using .NET WebClient or related objects

First of all, I'm completely open to suggestions for alternative strategies, since this is not a space I'm hugely familiar with.
I have what I think is a simple problem. I have a client app and a server app, and I want to send a bunch of files from the client to the server. I don't want to require the user to set up file shares or anything, so my thinking was:
I'll have an app on the "server" using HttpListener to listen to file transfer requests from the client.
I'll have an app on the client using WebClient.UploadFile to initiate the transfers.
Twist: The server is a Mac, and I need to use things that are supported by Mono. I'm not looking to have the server install ASP.NET / IIS / whatever. In fact, the less the user has to do, the better. (The user in this case will be in control of both the client and the server, by the way.)
So, short questions:
Is there a better/easier way than WebClient/HttpListener to send a bunch of files from one machine on a LAN to another? TCP-something? Sockets-something-or-other? (You can see my ignorance.)
Assuming I use HTTP, are there more straightforward ways to download files than using an HttpListener and manually parsing the requests to get rid of headers, etc.?

can clients see server side source code ? asp.net

I would like to know if somehow any client using any browser can see my server side code? if it is so how could I prevent this and actually what is the client able to see when he is visiting my web site?
(asp.net)
I've never used asp.net before, but i assume it's the same like PHP or Perl. The client only sees what the server is sending to him (like any other network application).
The normal order of execution of things should be ->A client sends a request to a specific resource. Your server interprets this request and opens the required files for processing. Then a parser goes through your source files and executes your code.
The only way of source code being send to the client is a typo where you miss the " at the end of your strings.

Download Docx file from local server machine to client?

In c# I've assigned to write code for download and upload docx file from local server machine to the same local client machine. Let me explain clearly,
For example: If i need to send docx file (C:\sample.docx ) to client machine into directory(D:\sample) using web server(Here using apache tomcat) .. Here using httpwebrequest & httpwebresponse classes..
Is there any possible to find out the solution?
Trying simple client - server application, It’s like a file sharing (Server assign word file to client and then client download that assigned file), I'm done with sockets-TCP/IP that’s works really fine. But trying to work out using http...
You cound not send something for Server to Client. Client have to request, Server have to respond.
This is a rather simple task. You can just place your file into web site folder (~/sites/mysite/simple.docx), if appache is configured and you requested http://mysite/simple.docx, file would be returned as response.
WebClient class DownloadFile method is very suitable for that.

Receiving data sent from client machine

I'm working on a project that has a C# app (running 24/7 as a server) on the client's machine). This app needs to send a file as a byte stream via POST to a server I am currently hosting on my home desktop.
Can this file be received by a C# app running on my server, or does it have to be as ASP app/page?
Also, I know how to send a bytestream via POST, but how will I set my server side app to listen for this incoming data?
I have never done something like this, so I'm looking for some pointers to get me started.
Thanks
Depending on what you need to do with the file, and when it's uploaded, there is nothing wrong with using a tiny asp.net app to do this.
If the only way you can get the file to the server is via http POST, why write your own service/daemon to run a listener? Unless you have some reason not to run it in IIS, I would do it there. There is the full stack available that way for authentication and so on if you should happen to need it. Besides, if you have to upload files via http post, I would bet you will end up wanting other methods available via HTTP as well..
You're looking for this guy:
http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
Using that, you'll create basically a mini HTTP server, listening on some port you specify. You'll then be able to post data to it, and process it accordingly (similar to if the processing code was in an ASPX code-behind).
Assuming your open to sending directly via TCP you can look at this example if you dont wanna go the HTTP way. I personaly like the NetworkStream Class, for sending data over the network painfree.

How to modify HTTP responses in a different .NET process

I have a standard web server that serve web pages.
I want to monitor traffic on port 80, intercept every http responses, and add/inject an additionnal header to them.
The process will then act like a proxy between the web server and the client.
Could you please give me some pointers? I'm already aware of SharpPCap, but I'm not sure where to start.
Note: I can't rely on the web server, I can't control it or change it's configuration. However I can install any other process on the same machine.
Thanks a million
I think that SharpPCap is an overkill here.
Try:
listen on a port (say 8080)
for each incoming connection, accept and open one to the server (original one, port 80)
pass everything that comes in from the client straight to the server
pass everything that comes from the server back to the client, monitoring the stream and injecting/modifying if needed
I think what you want to do can be done with IIS 7.0 URL Rewrite module instead of rolling your own code.
http://learn.iis.net/page.aspx/711/modifying-http-response-headers/

Categories

Resources