I have a C# form application which I want to have listening for incoming HTTP requests from other computers.
How would I go about doing this?
For simple needs, the HttpListener class is a good and simple choice. There is an example on the linked MSDN page.
If, for some reason, you cannot use HttpListener, the process would be to listen to a port using TcpClient (or even the sockets API if you need the gritty details), and then implement the HTTP Protocol. I highly recommend HttpListener over rolling your own, unless you have specific requirements that HttpListener does not meet.
You can use ASP.NET Http Filters to intercept HTTP requests.
See more details here
If it's an asp.net application, you can check the http requests in the Application_BeginRequest event handler of your global.asax.
Related
I'm developing an application in C# which should support requests from both HTTP clients & on sockets. Idea is to let users access functionality using HTTP and use sockets for IPC.
Is it possible to have both these interfaces supported in a process?
I would recommend using Windows Communication Foundation for the .NET framework.
http://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx
With it, you can host your application in IIS and offer both TCP and HTTP endpoints.
Yes, that is possible. You probably would open a socket manually for your custom transport, and use an HTTP server library to expose an HTTP endpoint.
Is it possible to have both these interfaces supported in a process?
Yes.
You can read the incoming data as a string, try to parse it using an HTTP library and then decide what to do based on whether it is or not.
I need to write an Http Handler to listen to port 80.
What should be the approach? Like should I use raw sockets to listen to port 80 within Http Handler or is there a better way to do?
I went through some tutorials as the one specified below..
http://msdn.microsoft.com/en-us/library/ms228090.aspx
But couldn't make much out of it. Any idea will appreciated.
Thanks
An HTTP handler is just a handler that gets called by an application host, such as Internet Information Services (IIS). IIS can listen on port 80 and invoke an HTTP handler, but it is not the HTTP Handler's job to listen on a port, or even care which port the application host is listening on.
What should be the approach? Like should I use raw sockets to listen to port 80 within Http Handler or is there a better way to do?
Just use IIS, or IIS Express. Create an HTTP handler, and let IIS listen on port 80.
OK. What you are looking at is a way to make a handler in IIS. If IIS isn't desired, consider using HttpListener to hook the HttpPipeline from any app.
Socket is a very flexible way to create HTTP handlers but you have to do more with sockets. if classes like HttpListener fullfill your requirement always go for them. unless it will be something like re-inventing the wheel.
This is a working code sample which shows how to use Sockets to listen TCP requests.
http://kaninotes.blogspot.com/2012/02/how-to-implement-threaded-server-socket.html
But go for HttpListener if you deal with http stuff.
Is it possible to get all HTTP requests of a browser?
For example: we have opened the browser, navigated to google.com, searched for a string, clicked on any link and I got some error.
Now I want to track all HTTP requests from opening browser.
We have been using 'fiddler' to do so. But we want to use C# code to track all HTTP requests as well as HTTP responses. And we want to use the failed HTTP responses in our program.
Any information on this will be very helpful.
You could use SharpPcap to implement your own 'sniffer', or use a HttpListener to create a proxy that forwards web requests.
AFAICS, there is no way of implementing this which does not replicate what you are already doing with fiddler (albeit that you could implement a passive sniffer rather than a proxy).
I've some fishy application that makes HTTP requests to a website, i would like to intersect that request and send other data to the server. Is that possible in C#,java or C++?
EDIT: The application isn't mine, i just know the endpoint that it sends http requests
Fiddler might provide the functionality you need. At the very least it may enable you to see what is being sent to the web site.
in Java You can intercept request from Filter
You may want to look into using an HttpModule, whose purpose is to intercept incoming HTTP requests.
The ASP Column: HTTP Modules
Firstly are you aware of how it is connecting to the internet? For example, is it using the settings from Internet Explorer, or is it establishing a direct connection? If the latter, this may be tricky, there is no direct port forwarding as there in Linux, so you'll need some third-party tools to redirect the traffic to a server (which you can write in Java, C++ or C#, I would go for C# if you know it for pure speed of development) In that server you can intercept the request, and then create your own to actually send to the real destination.
Sounds like a cludge, but I think you're stuck with this approach due to the lack of direct port forwarding. You'll have to configure the third-party tool that you use to forward someother well known port to 80, and your server should write to this.
I'm working with asp.net MVC. Now I don't know how to send data form a server (using asp.net ) to another server using (win32 console command line). Plz help me.
P/S: Is there any security hole in this method.
Well generally the case is that most ports today are blocked behind firewalls so setting something like that up with winsock, is outdated. If you are tring to connect two servers there are many options, You could look into the System.Web.WebClient,System.Net.HttpWebRequest,Microsoft's Sync Framework, Rhino queues but heres the run down on the first two.
In short, HttpWebRequest gives you more fine grained control over your
request. WebClient does not. It encapsulates most of the stuff for you.
WebClient is very useful if you want to do specialized, one-off tasks, eg:
download file, do forms post etc.
HttpWebRequest is useful if you want to do more complicated stuff.
The WebClient is especically simplified, we can use it's DownloadData,
DownLoadFile to retreive file/stream from remote webserver. Here are some
tech articles and resources describing using webclient or webrequest:
Hosting WCF services, WebClient
here
and webrequest here.
You have two servers trying to communicate. If you are going to use IP (I am assuming you will since you mentioned Winsock) you must choose between these two protocols:
TCP
UDP
Once you've decided which one to use, you can write a server process (the console application) that listen to a specific port (TCP or UDP port depending on what you've chosen) that will serve your client process (the ASP.NET application).
If you use TCP/IP, you use sockets to communicate. If you use UDP/IP, you will send and receive independent packets.
Here is a TCP/IP client/server code sample in C# you can use. You will wrap and run the client portion of this example is a class you can access within ASP.NET MVC.
Here is a UDP/IP server code sample in C#.
Regarding your question of the security of this approach, the question does not provide enough information to answer it properly. You will need to provide more information.