How to implemt a simple web listener in asp.net/vb.net

J

Jerome Cohen

I need to implement a simple application that listens for web requests on a
well-known URL, grab the request , which is an XML document, and send as
respnse an XML document.
each client sends a first request and I return failure or a session ID which
I store, and further requests should have the session ID.

isn't there a sim[ple way to do it? I keep reading about HTTP handlers.
isn't there a simpler way to implement it?
 
J

Jerome Cohen

thanks.

well, I do want to use ASP.NET/VB.NET. does it mean I have to use the HTTP
handlers method ?
my server is running WIN 2K


Brock Allen said:
If you don't need ASP.NET then you can host HTTP directly in an application
fairly easily using .NET 2.0 with http.sys (so Windows 2003 or XP SP2). Here's
a sample in C# that you should be able to morph to do XML. The trick is using
HttpListenerContext.Request and Response to do the heavy lifting for you

using System.IO;
using System.Net;

class Program
{
static void Main(string[] args)
{
HttpListener l = new HttpListener();
l.Prefixes.Add("http://localhost/HttpListenerApp/");
l.Start();
while (true)
{
HttpListenerContext ctx = l.GetContext();
string url = ctx.Request.RawUrl;
StreamWriter w = new
StreamWriter(ctx.Response.OutputStream);
w.WriteLine("<h2>The URL was {0}</h2>", url);
w.Close();
}
}
}





I need to implement a simple application that listens for web requests
on a
well-known URL, grab the request , which is an XML document, and send
as
respnse an XML document.
each client sends a first request and I return failure or a session ID
which
I store, and further requests should have the session ID.
isn't there a sim[ple way to do it? I keep reading about HTTP
handlers. isn't there a simpler way to implement it?
 
B

Brock Allen

If you don't need ASP.NET then you can host HTTP directly in an application
fairly easily using .NET 2.0 with http.sys (so Windows 2003 or XP SP2). Here's
a sample in C# that you should be able to morph to do XML. The trick is using
HttpListenerContext.Request and Response to do the heavy lifting for you

using System.IO;
using System.Net;

class Program
{
static void Main(string[] args)
{
HttpListener l = new HttpListener();
l.Prefixes.Add("http://localhost/HttpListenerApp/");
l.Start();
while (true)
{
HttpListenerContext ctx = l.GetContext();
string url = ctx.Request.RawUrl;
StreamWriter w = new
StreamWriter(ctx.Response.OutputStream);
w.WriteLine("<h2>The URL was {0}</h2>", url);
w.Close();
}
}
}
 
B

Brock Allen

Ah, ok. I was just offering that as a non-IIS/ASP.NET approach as an idea.
So if you are using ASP.NET, then yes, implementing a IHttpHandler would
probabaly be your best bet. They're quite simple once you get started.




thanks.

well, I do want to use ASP.NET/VB.NET. does it mean I have to use the
HTTP
handlers method ?
my server is running WIN 2K
If you don't need ASP.NET then you can host HTTP directly in an
application

fairly easily using .NET 2.0 with http.sys (so Windows 2003 or XP
SP2).
Here's

a sample in C# that you should be able to morph to do XML. The trick
is
using

HttpListenerContext.Request and Response to do the heavy lifting for
you

using System.IO;
using System.Net;
class Program
{
static void Main(string[] args)
{
HttpListener l = new HttpListener();
l.Prefixes.Add("http://localhost/HttpListenerApp/");
l.Start();
while (true)
{
HttpListenerContext ctx = l.GetContext();
string url = ctx.Request.RawUrl;
StreamWriter w = new
StreamWriter(ctx.Response.OutputStream);
w.WriteLine("<h2>The URL was {0}</h2>", url);
w.Close();
}
}
}
I need to implement a simple application that listens for web
requests
on a
well-known URL, grab the request , which is an XML document, and
send
as
respnse an XML document.
each client sends a first request and I return failure or a session
ID
which
I store, and further requests should have the session ID.
isn't there a sim[ple way to do it? I keep reading about HTTP
handlers. isn't there a simpler way to implement it?
 
J

Jerome Cohen

thanks again!

assuming I have a page, "/dir1/mypage.aspx" that will implement the handler.
I understand I need to declare the public class that will implement the
handler
Public Class MyHandler

Implements IHttpHandler

and implement "IsReusable" and "ProcessRequest"

now, what exactly do I need to do on my web.configto make the handler
receive the requests to mypage.aspx? or does it does it have to be
machine.config? anything else I am missing?
all the examples I saw assume so much besides the basic HTTP handling its
hard to differenciate the "pure" HTTP handler.


Brock Allen said:
Ah, ok. I was just offering that as a non-IIS/ASP.NET approach as an idea.
So if you are using ASP.NET, then yes, implementing a IHttpHandler would
probabaly be your best bet. They're quite simple once you get started.




thanks.

well, I do want to use ASP.NET/VB.NET. does it mean I have to use the
HTTP
handlers method ?
my server is running WIN 2K
If you don't need ASP.NET then you can host HTTP directly in an
application

fairly easily using .NET 2.0 with http.sys (so Windows 2003 or XP
SP2).
Here's

a sample in C# that you should be able to morph to do XML. The trick
is
using

HttpListenerContext.Request and Response to do the heavy lifting for
you

using System.IO;
using System.Net;
class Program
{
static void Main(string[] args)
{
HttpListener l = new HttpListener();
l.Prefixes.Add("http://localhost/HttpListenerApp/");
l.Start();
while (true)
{
HttpListenerContext ctx = l.GetContext();
string url = ctx.Request.RawUrl;
StreamWriter w = new
StreamWriter(ctx.Response.OutputStream);
w.WriteLine("<h2>The URL was {0}</h2>", url);
w.Close();
}
}
}

I need to implement a simple application that listens for web
requests
on a
well-known URL, grab the request , which is an XML document, and
send
as
respnse an XML document.
each client sends a first request and I return failure or a session
ID
which
I store, and further requests should have the session ID.
isn't there a sim[ple way to do it? I keep reading about HTTP
handlers. isn't there a simpler way to implement it?
 
B

Brock Allen

Just add a <httpHandlers> section to your web.config:

<configuration>
<system.web>
<httpHandlers>
<add path="YourURL.aspx" verb="POST" type="YourNamespace.YourClass, YourAssemblyWithoutTheDotDll"
/>
</httpHandlers>
</system.web>
</configuration>

For the verb I'd use POST since you said the client would be passing up XML
in the body. You can just put verb="*" if they might also do a GET.




thanks again!

assuming I have a page, "/dir1/mypage.aspx" that will implement the
handler.
I understand I need to declare the public class that will implement
the
handler
Public Class MyHandler
Implements IHttpHandler

and implement "IsReusable" and "ProcessRequest"

now, what exactly do I need to do on my web.configto make the handler
receive the requests to mypage.aspx? or does it does it have to be
machine.config? anything else I am missing?
all the examples I saw assume so much besides the basic HTTP handling
its
hard to differenciate the "pure" HTTP handler.
Ah, ok. I was just offering that as a non-IIS/ASP.NET approach as an
idea. So if you are using ASP.NET, then yes, implementing a
IHttpHandler would probabaly be your best bet. They're quite simple
once you get started.

thanks.

well, I do want to use ASP.NET/VB.NET. does it mean I have to use
the
HTTP
handlers method ?
my server is running WIN 2K
If you don't need ASP.NET then you can host HTTP directly in an

application

fairly easily using .NET 2.0 with http.sys (so Windows 2003 or XP
SP2).

Here's

a sample in C# that you should be able to morph to do XML. The
trick is

using

HttpListenerContext.Request and Response to do the heavy lifting
for you

using System.IO;
using System.Net;
class Program
{
static void Main(string[] args)
{
HttpListener l = new HttpListener();
l.Prefixes.Add("http://localhost/HttpListenerApp/");
l.Start();
while (true)
{
HttpListenerContext ctx = l.GetContext();
string url = ctx.Request.RawUrl;
StreamWriter w = new
StreamWriter(ctx.Response.OutputStream);
w.WriteLine("<h2>The URL was {0}</h2>", url);
w.Close();
}
}
}

I need to implement a simple application that listens for web
requests
on a
well-known URL, grab the request , which is an XML document, and
send
as
respnse an XML document.
each client sends a first request and I return failure or a
session
ID
which
I store, and further requests should have the session ID.
isn't there a sim[ple way to do it? I keep reading about HTTP
handlers. isn't there a simpler way to implement it?
 
J

Jerome Cohen

its working!



Brock Allen said:
Just add a <httpHandlers> section to your web.config:

<configuration>
<system.web>
<httpHandlers>
<add path="YourURL.aspx" verb="POST" type="YourNamespace.YourClass, YourAssemblyWithoutTheDotDll"
/>
</httpHandlers>
</system.web>
</configuration>

For the verb I'd use POST since you said the client would be passing up XML
in the body. You can just put verb="*" if they might also do a GET.




thanks again!

assuming I have a page, "/dir1/mypage.aspx" that will implement the
handler.
I understand I need to declare the public class that will implement
the
handler
Public Class MyHandler
Implements IHttpHandler

and implement "IsReusable" and "ProcessRequest"

now, what exactly do I need to do on my web.configto make the handler
receive the requests to mypage.aspx? or does it does it have to be
machine.config? anything else I am missing?
all the examples I saw assume so much besides the basic HTTP handling
its
hard to differenciate the "pure" HTTP handler.
Ah, ok. I was just offering that as a non-IIS/ASP.NET approach as an
idea. So if you are using ASP.NET, then yes, implementing a
IHttpHandler would probabaly be your best bet. They're quite simple
once you get started.


thanks.

well, I do want to use ASP.NET/VB.NET. does it mean I have to use
the
HTTP
handlers method ?
my server is running WIN 2K
If you don't need ASP.NET then you can host HTTP directly in an

application

fairly easily using .NET 2.0 with http.sys (so Windows 2003 or XP
SP2).

Here's

a sample in C# that you should be able to morph to do XML. The
trick is

using

HttpListenerContext.Request and Response to do the heavy lifting
for you

using System.IO;
using System.Net;
class Program
{
static void Main(string[] args)
{
HttpListener l = new HttpListener();
l.Prefixes.Add("http://localhost/HttpListenerApp/");
l.Start();
while (true)
{
HttpListenerContext ctx = l.GetContext();
string url = ctx.Request.RawUrl;
StreamWriter w = new
StreamWriter(ctx.Response.OutputStream);
w.WriteLine("<h2>The URL was {0}</h2>", url);
w.Close();
}
}
}

I need to implement a simple application that listens for web
requests
on a
well-known URL, grab the request , which is an XML document, and
send
as
respnse an XML document.
each client sends a first request and I return failure or a
session
ID
which
I store, and further requests should have the session ID.
isn't there a sim[ple way to do it? I keep reading about HTTP
handlers. isn't there a simpler way to implement it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top