473,394 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
2 2663
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alchamist | last post by:
Hi all, I am writing a simple app to provide an simple GUI to write windows netlogon scripts. Is there a way in vb.net to provide a button on a form to open the "Active Directory Users and...
2
by: FrodoBaggins | last post by:
Dear Team, I am running Visual Studio 2003 Version 7.1.3088 on Windows Server 2003. I have written a C# application that must write to the event log. When it attempts to write to the event log,...
0
by: sztonix | last post by:
Hi all, I encountered a crystal report deployment problem. I make reports with Crystal Report for Visual Studio .NET 2003. It works fine in the development machine. And then make setup...
2
by: Tobias Johansson | last post by:
Hello, I'm having what I believe a security problem to execute an executable file from a windows service in windows server 2003. It works fine in WIN XP SP2 The program(the service) itself...
7
by: Lucas | last post by:
Hi, I have an ASP.Net application written with VS.Net 2002 (Net FWK 1.0). This Web Application uses Exception Management Application Block to log Events to Windows Event Log. We registered the...
1
by: Steffen Loringer | last post by:
Hi, my asp.net application is running fine on my development system. The application opens an access database and needs therefore write permissions. If I copy my complete folder on a Windows...
2
by: Jared Hagel | last post by:
I've searched the web for a solution to this problem. Surprisingly, no problem/solution has been posted yet. We can read application configuration information fine when our asp.net application...
1
by: Simon Verona | last post by:
Hope somebody can help. I have a web service written in vb.net which works fine localling on my Windows XP Pro development machine without a problem. I have deployed in onto a Windows Server...
0
by: Daniel | last post by:
Ive used the New Serial Port Namespace in Net Framework 2 to write a Serial Port Monitoring Application. Basically the program opens the serial port and listens for any data that is received. It...
2
by: =?Utf-8?B?SDIw?= | last post by:
I'm having a problem with the access control in windows server 2003. I have set in our server that user can access, modify, read and write folders/files but not able to delete folders, subfolders...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.