472,807 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 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 2622
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.