views:

50

answers:

2

Hi,

Does any one know, how to communicate to Apache server from client side using Win32 or C#. and can i get the control to execute the process at server? if so how to do?. how to receive file sent by apache?.

Please help me...

Thanks

+3  A: 

You can use HTTPWebRequest and HTTPWebResponse to make a request to a web server and receive a response.

For example

static void Main(string[] args)
{
 // used to build entire input
 StringBuilder sb  = new StringBuilder();
 // used on each read operation
 byte[]        buf = new byte[8192];
 // prepare the web page we will be asking for
 HttpWebRequest  request  = (HttpWebRequest)
  WebRequest.Create("http://www.mayosoftware.com");
 // execute the request
 HttpWebResponse response = (HttpWebResponse)
  request.GetResponse();
 // we will read data via the response stream
 Stream resStream = response.GetResponseStream();

 string tempString = null;
 int    count      = 0;
 do
 {
  // fill the buffer with data
  count = resStream.Read(buf, 0, buf.Length);
  // make sure we read some data
  if (count != 0)
  {
   // translate from bytes to ASCII text. 
                            // Not needed if you'll get binary content.
   tempString = Encoding.ASCII.GetString(buf, 0, count);
   // continue building the string
   sb.Append(tempString);
  }
 }
 while (count > 0); // any more data to read?
 // print out page source
 Console.WriteLine(sb.ToString());
}
Vinko Vrsalovic
+2  A: 

Okay. Assuming you are talking about "Apache httpd", then we are talking about a server which talks HTTP. Pretty much any programming language has some "HTTP Client" classes. In the case of C#, you can use HttpWebRequest. Vinko gave a great "copy/paste" example.

To run a process on the server-side, you need some "server side scripting". If there is no module available for the httpd server, then you can always resort to CGI. This is potentially dangerous, if not careful though. But you can say this about all server-side scripting (the "if not careful" is important here).

As it sounds like, you are new to dynamic web development. And, as much as I dislike it, I have to say that the PHP community is a great starting point.

To give a rough overview of how things work:

  1. The client (You C# app) make a "request" to the server (by default a TCP connection to port 80)
  2. As the server (httpd) is listening on this port, it accepts this connection and starts "talking HTTP"
  3. The server retrieves the URL from the HTTP GET command and decides what to do with it.
  4. If this URL maps to a static resource, the server sends a "response" with the resource's content and is done.
  5. If this URL maps to a dynamic resource, the server delegates the work to another process. This process executes, and the http-server receives the standard output of the process as result (In the case of PHP, CGI, and other similar technologies). The server then sends this result back to the client.

What I understood the least when I got started were the "cryptic" httpd-configurations. They are simple to understand though. They are there to tell the server how (and what) to map to the external process (as noted in step 4a and 4b).

So, in the case of PHP:

LoadModule php5_module modules/libphp5.so

This makes the "external process" and configuarion directives available to the server (loads the extensions).

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

The default Apache-httpd handler assumes that a URL maps to a File on the file system. This configuration directive tells httpd to process (handle) each filename ending with ".php" using the PHP module before returning it to the client.

Final note

These steps are very similar for other server-side applications. I only gave PHP as a practical example. And it's got very good documentation.

exhuma
Thanks Exhuma..i agree,, i never worked on this php. so i am in lot of confusion about server side programming :-(.. i have a small quearyif i create a .aspx page at server and through that i can run my script present in server right?.from my desktop app ill call the my .asp page using "Vinko Vrsalovic" Method,in my .asp page i can execute script and get the results right... if am wrong suggest me
Shadow
You are correct. - Your .aspx file is executed on the server (hence "server side scripting"). So from there you can access any resources available on it. Due to security concerns, some access may be restricted by default. This depends on the handler itself (For example, PHP has it's php.ini file). Some settings can also be changed directly within the httpd configuration. But most tasks should work even with those restrictions in place.
exhuma