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

Need to write an HTTP server , can i use a webservice with "NOSOAP"?

P: n/a

Hello,
I got this "breath taking" task to write a an http server to which "xml
data" will be posted to and will answer with xml data.
The logic behind the xml processing is not a matter here.

My question is : Can i configure a webservice for HHTP POST ?

The remote peer just performs an http web request.
NO RPC style call to my system.
How then can i authenticate with username and password?
Would it be better no to use webservice.

Where can i see good examples of http servers with authentication written in
C#?
Any help will be very much appreciated.
JJ
Apr 25 '06 #1
Share this Question
Share on Google+
29 Replies


P: n/a
Well, you could just post to an ashx; pretty easy to setup - simply read
from the Request and write to the Response...

Marc
Apr 25 '06 #2

P: n/a

"Marc Gravell" <ma**********@gmail.com> skrev i en meddelelse
news:O3**************@TK2MSFTNGP04.phx.gbl...
Well, you could just post to an ashx; pretty easy to setup - simply read
from the Request and write to the Response...

Marc


This sounds great!

Can you give more info on where to read about this!
Apr 25 '06 #3

P: n/a
Easiest thing is just to create a new (empty) web-site, add an ashx (generic
handler), and play with it; for example (writes, doesn't read - but easy to
do using Load and the Request.InputStream) see below. Note that for large
xml (in the Mb range) you may wish to avoid the XmlDocument class, as it has
a performance (mainly memory) overhead; using XmlReader / XmlWriter is far
more efficient, but also (much) trickier to get right. XmlDocument is fine
for small to mid-size xml, plus it gives you XPath / XQuery.

Hope this helps,

Marc

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/xml";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(@"<Test Of=""Some""><Xml/></Test>");
doc.Save(context.Response.OutputStream);
}

public bool IsReusable {
get {
return false;
}
}

}
Apr 25 '06 #4

P: n/a
To your other questions:
How then can i authenticate with username and password?
For this type of approach without SOAP? If you are using SSL, one option for
username / pw is to just use credentials in the XML - i.e.

<SomeMessage cn="who" pw="whatever">
<!-- blah -->
</SomeMessage>
Would it be better no to use webservice.
Not if it doesn't meet the requirement ;-p
I have happily used this approach for inter-business communications without
any hitches; sometimes it was our design, sometimes theirs, so it isn't just
me...
But yes, web-services do give you many other options, in particular for
security and schema definition. Also handy if you don't have direct
line-of-sight between client and server, but this isn't very common.
... to write a an http server ...


Can you not just use IIS? If you do actually need to write the server, then
you can do a lot with the HttpListener class... but I wouldn't unless I
really had to ;-p (the main time I have used this is for client-side things
similar to how google desktop search works...)

Apr 25 '06 #5

P: n/a
>
Marc

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/xml";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(@"<Test Of=""Some""><Xml/></Test>");
doc.Save(context.Response.OutputStream);
}

public bool IsReusable {
get {
return false;
}
}

}


This looks amazingly like what i need. If the user sends a client
certificate.
can i use the code below here?

ServicePointManager.ServerCertificateValidationCal lback += new
System.Net.Security.RemoteCertificateValidationCal lback(ValidateServerCert);

Many thanks
JJ
Apr 25 '06 #6

P: n/a
No idea, sorry. I haven't had the need / opportunity to play with client
certificates in this context. It can probably be done, though. I would first
look at MSDN2 and google.

Anybody [else] out there experienced with such?

Marc
Apr 25 '06 #7

P: n/a
>> public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/xml";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(@"<Test Of=""Some""><Xml/></Test>");
doc.Save(context.Response.OutputStream);
}

public bool IsReusable {
get {
return false;
}
}

This looks great.

Can i deploy it like any other website?


Apr 26 '06 #8

P: n/a
Yup; it's just a page; as long as the web server supports asp.net it should
workm just fine; can deploy either as a standalone page, or as precompiled

(caveat: it /might/ work in 1.1 (I simply haven't tried), but certainly
works in 2.0)

Marc
Apr 26 '06 #9

P: n/a
Many thanks Marc,
I think the industry need more people like you ;)

Apr 26 '06 #10

P: n/a
How do i actually produce a response from an httphandler.

I get error: (405) Method Not Allowed

Apr 26 '06 #11

P: n/a
Well, what is the "method"? GET, POST? what?

And how are you seeing the error?

The following works with the previous code via GET (obviously change the
uri):

static void Main(string[] args) {
XmlDocument doc = new XmlDocument();
using (WebClient client = new WebClient()) {
using(Stream stream =
client.OpenRead("http://localhost:1133/WebSite1/Handler.ashx")) {
doc.Load(stream); }
}
Console.WriteLine(doc.OuterXml);
}

A few quick tests POSTing with UploadData proved abortive, but I am pretty
certain you can use HttpWebRequest to do this; more code, though...

Marc
Apr 26 '06 #12

P: n/a
I'm using
System.Net.WebResponse resp = (HttpWebResponse)req.GetResponse();

with a given uri.
Apr 26 '06 #13

P: n/a
This seems to work with the previously posted ashx running on the standard
web-server:

// GET
HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("http://localhost:1133/WebSite1/Handler.ashx");
req.ContentType = "text/xml"; // maybe a white lie... ;-p (since
no body)
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
XmlDocument doc = new XmlDocument();
using (Stream fetched = resp.GetResponseStream()) {
doc.Load(fetched);
}
Console.WriteLine(doc.OuterXml);

// POST
req =
(HttpWebRequest)WebRequest.Create("http://localhost:1133/WebSite1/Handler.ashx");
req.ContentType = "text/xml";
req.Method = "POST";
doc.LoadXml("<Something><Posted/></Something>");
using (Stream post = req.GetRequestStream()) {
doc.Save(post);
}
resp = (HttpWebResponse)req.GetResponse();
using (Stream fetched = resp.GetResponseStream()) {
doc.Load(fetched);
}

Console.WriteLine(doc.OuterXml);
Console.ReadLine();
Apr 26 '06 #14

P: n/a
> doc.LoadXml("<Something><Posted/></Something>");
using (Stream post = req.GetRequestStream()) {
doc.Save(post);
}
resp = (HttpWebResponse)req.GetResponse();
using (Stream fetched = resp.GetResponseStream()) {
doc.Load(fetched);
}

Console.WriteLine(doc.OuterXml);
Console.ReadLine();

I think io was missing the ahsx filename in my url.
Will try again
Apr 26 '06 #15

P: n/a
Hi Marc,

Have you ever encountered any (404) not found error while connecting to the
"httphandler"?

below my code: (message contain a clob xml retreived from an oracle db)

HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(Configuratio nManager.AppSettings["URI"]);
req.ContentType = "text/xml; charset=utf-8";

req.Method = "POST";

UTF8Encoding encoding = new UTF8Encoding();

byte[] postBytes = encoding.GetBytes(message);

req.ContentLength = postBytes.Length;

System.IO.Stream reqStream = req.GetRequestStream();

reqStream.Write(postBytes, 0, postBytes.Length);

reqStream.Close();

System.IO.File.WriteAllBytes("BeingSent_"+message_ id.ToString() +
"PostData.xml", postBytes);


System.Net.WebResponse resp = (HttpWebResponse)req.GetResponse();

System.IO.StreamReader sr = new
System.IO.StreamReader(resp.GetResponseStream());

message = (sr.ReadToEnd().Trim()).ToString();
Apr 27 '06 #16

P: n/a
Well, if I did, the first thing I would do is write the URI to either Debug
or Console, and then try and hit it separately... it often means that *it
isn't there* ;-p

If the file is physically there but unreachable, then you might want to
check security and web.config; for instance, are you using a
HttpNotFoundHandler (or whatever it is called) for anything, or is your
handler under App_Code or something else unreachable. Also maybe IIS
security settings, and ISAPI filters (URLScan for instance), etc.

Marc

Apr 27 '06 #17

P: n/a
You are i correct, i get the same answer with IE:

http://localhost/server/handler.ashx

Server Error in '/' Application.
--------------------------------------------------------------------------------

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make sure that
it is spelled correctly.

Requested URL: /server/handler.ashx
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET
Version:2.0.50727.42
Apr 27 '06 #18

P: n/a
Are you running through IIS or Cassini? (Cassini is the virtual web-server
that ships with VS2005, and which is the default for web-projects running
through the IDE)

If Cassini, you will need to include the port number in the URI, as Cassini
does not default to port 80 - and IIRC Cassini only supports local requests
(which, OK, is the case in your example).

See the URIs in my earlier post for an example of this; note that Cassini
has a habit of generating the port numbers dynamically - although they can
be fixed within a solution. The best option is to run the web-site through
IIS if you have it installed. The IIS thing can still be done through VS2005
(including breakpoints etc), as long as you:
a: Make sure that IIS has a suitable path (URI) mapped to your working
project folder
b: Make sure that VS2005 knows what this URI is in the "Use custom server"
option in the "Start Options" for the web-project

Any use?

Marc
Apr 27 '06 #19

P: n/a
Of course, the best way to get the correct URI is to right-click on the ashx
in the IDE and select "View in Browser"; no copy/paste the URI from IE ;-p

Marc
Apr 27 '06 #20

P: n/a

"Marc Gravell" <ma**********@gmail.com> skrev i en meddelelse
news:u0**************@TK2MSFTNGP04.phx.gbl...
Of course, the best way to get the correct URI is to right-click on the
ashx in the IDE and select "View in Browser"; no copy/paste the URI from
IE ;-p

Marc


I have double checked averything.
I'm of course developing on Cassini but i did map it to a virtual directory
under IIS.

still 404

..I wonder if it would be easier to write a socket server listening on on
port 443 since the system requires client authentication
Apr 27 '06 #21

P: n/a
I found an interesting article on msdn2:

http://msdn2.microsoft.com/en-us/lib...sslstream.aspx

Since i also need authentitcation via "client side certificate", couldnt
this be what i need ?

Many thanks for your help.

JJ
Apr 27 '06 #22

P: n/a

"jens Jensen" <je**@jensen.dk> skrev i en meddelelse
news:ue**************@TK2MSFTNGP05.phx.gbl...
I found an interesting article on msdn2:

http://msdn2.microsoft.com/en-us/lib...sslstream.aspx

Since i also need authentitcation via "client side certificate", couldnt
this be what i need ?

Many thanks for your help.

JJ


I can see that is more hassle then using the httphandler.
Apr 28 '06 #23

P: n/a

"jens Jensen" <je**@jensen.dk> skrev i en meddelelse
news:ue**************@TK2MSFTNGP05.phx.gbl...
I found an interesting article on msdn2:

http://msdn2.microsoft.com/en-us/lib...sslstream.aspx

Since i also need authentitcation via "client side certificate", couldnt
this be what i need ?

Many thanks for your help.

JJ


I can see that is more hassle then using the httphandler.
Apr 28 '06 #24

P: n/a
IMO, if you *have* IIS, *develop* under IIS, otherwise you're just setting
yourself up for a world of hurt.

Drop Cassini, and set the custom server settings. If you can't right-click /
browse on the file in the IDE and have it load correctly, then your IIS
configuration is incorrect. Not a code issue, but a config one.

To rule this out, switch back to Cassini only; browse and capture the
Cassini path (inc. port); point your client at that URI; if this works, then
your code isn't the issue: IIS is.

Marc

Apr 28 '06 #25

P: n/a
>
To rule this out, switch back to Cassini only; browse and capture the
Cassini path (inc. port); point your client at that URI; if this works,
then your code isn't the issue: IIS is.

Marc


I have done this. right now, my ProcessRequest (HttpContext context)
contains no code.
I can browse.

Now my client says, the remote server has timed out.

Apr 28 '06 #26

P: n/a
How do i actually choose IIS as dev webserver. I have now spent some time
trying to figure out how to do it.
Thanks
JJ
Apr 28 '06 #27

P: n/a
Well, I kinda already told you...

First step: ensure you really have the IIS path to the project root
1: Configure IIS and get so you can see (in the IIS explorer) the ashx
2: Right-click / browse on the ashx so it loads in IE (it really should
load; if not IIS is knackered)
3: Copy the URL from IE, and cut off the bits after your project root - e.g.
if your ashx is in the root of the project, then keep everything except for
/your.ashx; if your ashx is one level deep keep everything except
/something/your.ashx - etc

Now configure VS:
1: Right-click on the web-project; property pages; start options
2: Change the "Server" to "Use custom server", and paste your project-root
uri into the "Base URL"

Test:
1: Right-click / View in browser on the ashx in VS; it should load again
correctly at the right URL
2: Update any Cassini urls in your project to use the IIS url
3: Go to solution properties; ensure that the client and the web server are
in the startup ("multiple startup projects")
4: (optional) in the web-project properties, set the start page to "None" or
whatever (to wait for a client)
5: Put a break point into the web-project and hit play

Honestly, it really can work!

Marc
Apr 28 '06 #28

P: n/a
>
Honestly, it really can work!

Marc


Your description had a millitary precison. And of course it does work!!!!
Apr 28 '06 #29

P: n/a
Well, since this is the 29th post in this chain, I thought I'd better bullet
point it...

Glad that helped;

This chain has switched topic a few times (xml / httpwebrequest / IIS etc);
if you have any more problems, can I suggest you discuss the specific
problem in a new thread? (just to keep each one "clean").

Best of luck,

Marc
Apr 28 '06 #30

This discussion thread is closed

Replies have been disabled for this discussion.