HttpListener and a client error (403)

I am attempting to create a al client/server app using the HttpListener class. I want to bypass IIS altogether. I was told that a system with SP2 has the necessary HTTP stack implemented in HTTP.SYS....so here is my simple, sample that I can't seem to get working. Initially I was going to create my server using sockets (SSL etc), but had read that it would be easier to user a higher level protocol like HTTP. So here are my first tentantive steps (cobbled together from googling). Any guidance would be appreciated, even a book reference would be welcome....


namespace MDA.HTTPServer
{

public class httpServer
{

public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}


// Create a listener.
HttpListener listener = new HttpListener();
listener.AuthenticationSchemes = AuthenticationSchemes.None;
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}

listener.Start();
Console.WriteLine("Listening...");

// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "Hello world!";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}
public static void Main()
{
string[] prefixes = { "http://localhost:8080/" };
SimpleListenerExample(prefixes);
System.Console.WriteLine("Done...");
System.Console.ReadLine();
}
}
}



AND Here is some client code to connect to listener...

namespace MDA.HTTPClient
{
public class MyHttpClient
{
public static void Main()
{
new MyHttpClient();
System.Console.WriteLine("Done...");
System.Console.ReadLine();
}
public MyHttpClient() {

string strUrl = "http://localhost:8080/"; //fails


System.Net.HttpWebRequest httpWebRequest =
(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strUrl);

string postData = "RequestFile";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

httpWebRequest.Method = "POST";
httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.ContentType = "text/plain";
httpWebRequest.ContentLength = byteArray.Length;

Stream dataStream = httpWebRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

System.Net.HttpWebResponse httpWebResponse =
(System.Net.HttpWebResponse)httpWebRequest.GetResponse();
System.IO.Stream receiveStream = httpWebResponse.GetResponseStream();

}
}

Thanks...
MDA


Answer this question

HttpListener and a client error (403)

  • HyperLethargy

    None No authentication is allowed. A client requesting an HttpListener object with this flag set will always receive a 403 Forbidden status. Use this flag when a resource should never be served to a client.
    Self inflicted pain :).

  • HttpListener and a client error (403)