By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

Write a "Windows Service" as a HTTP Request Server?

P: n/a
Hi there.

My goal is to write a windows service that can act as HTTP Request
server. How can I do that?

I know that I can use ASP.NET to develop a web site to achieve this
purpose, i.e. receive HTTP request. However, I'd like it to exist in
the form of a "Windows Service".
I also understand that we can use System.Net.WebRequest to send HTTP
Request (client), but how can I receive HTTP request (server)?

Thanks for your help in advance
Dom

Mar 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
http://www.google.sk/search?hl=sk&q=...ver+c%23&meta=
<do****@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi there.

My goal is to write a windows service that can act as HTTP Request
server. How can I do that?

I know that I can use ASP.NET to develop a web site to achieve this
purpose, i.e. receive HTTP request. However, I'd like it to exist in
the form of a "Windows Service".
I also understand that we can use System.Net.WebRequest to send HTTP
Request (client), but how can I receive HTTP request (server)?

Thanks for your help in advance
Dom

Mar 20 '06 #2

P: n/a
I have made an example for you.

Make a new console application.
Add references to"System.configuration", "System.Configuration.Install",
"System.ServiceProcess" and "System.Web"

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using System.Configuration.Install;

namespace WebRequestService
{
class Program : System.ServiceProcess.ServiceBase
{
public static String navn = "My Web log";
private bool RunThread = true;

public void StartMe()
{
System.Net.IPAddress localAddr =
System.Net.IPAddress.Parse("127.0.0.1");
System.Net.Sockets.TcpListener server = new
System.Net.Sockets.TcpListener(localAddr, 1234);
server.Start();
Byte[] bytes = new Byte[1024];
String data = null;
while (RunThread)
{
System.Net.Sockets.TcpClient client =
server.AcceptTcpClient();
data = null;
System.Net.Sockets.NetworkStream stream =
client.GetStream();
stream.Read(bytes, 0, bytes.Length);
data = System.Text.Encoding.ASCII.GetString(bytes);

System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\MyLog.txt", true);
sw.WriteLine(data);
sw.Close();

client.Close();
}
}
protected override void OnStart(string[] args)
{
System.Threading.Thread thr = new System.Threading.Thread(new
System.Threading.ThreadStart(this.StartMe));
thr.Start();
base.OnStart(args);
}

protected override void OnStop()
{
RunThread = false;
base.OnStop();
}
static void Main(string[] args)
{
// Register server in WindowsServices
// install : c:\> installutil /LogToConsole=false
cmd_Server.exe
// uninstall : c:\> installutil /LogToConsole=false /u
cmd_Server.exe
System.ServiceProcess.ServiceBase.Run(new Program());
}
}

[System.ComponentModel.RunInstallerAttribute(true)]
public class Installer : System.Configuration.Install.Installer
{
public Installer()
{
ServiceInstaller ServiceInstaller = new ServiceInstaller();
ServiceProcessInstaller ProcessInstaller = new
ServiceProcessInstaller();
ProcessInstaller.Account = ServiceAccount.LocalSystem;
ServiceInstaller.StartType = ServiceStartMode.Automatic;
ServiceInstaller.ServiceName = "My web log";
Installers.Add(ServiceInstaller);
Installers.Add(ProcessInstaller);
}
}
}
Compile it and open a cmd.
Type:
C:\Lars-Inge\WebRequestService\bin\Debug>installutil /LogToConsole=false
WebRequestService.exe
To install it as a Windows Service.

To uninstall it, type:
C:\Lars-Inge\WebRequestService\bin\Debug>installutil /LogToConsole=false /u
WebRequestService.exe

Open the Control Panel + "Administrative tools" + "Services".
Locate "My web log" and right click it and choose "start".

Your Windows Service web listener should now be running.
To test it:
open IE and navigate to
http://localhost:1234/hhhh
http://localhost:1234/skjshksajfkkkkttttttgwgggggtggssw
http://localhost:1234/kjfkdjf

Open the file explorer to see the result "c:\MyLog.txt".

GET /skjshksajfkkkkttttttgwgggggtggggss HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: no
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322; .NET CLR 2.0.50727; InfoPath.1)
Host: localhost:1234
Connection: Keep-Alive
Regards,
Lars-Inge Tønnessen
Siemens Medical - Norway
Mar 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.