views:

1071

answers:

4

Hi all,

I have a socket that is receiving HTTP requests.

So I have a raw http request in byte[] form from my socket.

I have to study this request - BUT

Instead of reinventing the wheel - can I 'cast' this byte array into a System.Net.HttpWebRequest or something similar?

Thanks!

----- UPDATE ---------

So anyway I couldn't find the answer. By digging a little further though I think it could be done by calling functions in:

HttpApi.dll I think that the HttpWebRequest uses this dll (winxpsp2)

the interesting structure is the HTTP_REQUEST

C++
typedef struct _HTTP_REQUEST {
  ULONG                  Flags;
  HTTP_CONNECTION_ID     ConnectionId;
  HTTP_REQUEST_ID        RequestId;
  HTTP_URL_CONTEXT       UrlContext;
  HTTP_VERSION           Version;
  HTTP_VERB              Verb;
  USHORT                 UnknownVerbLength;
  USHORT                 RawUrlLength;
  PCSTR                  pUnknownVerb;
  PCSTR                  pRawUrl;
  HTTP_COOKED_URL        CookedUrl;
  HTTP_TRANSPORT_ADDRESS Address;
  HTTP_REQUEST_HEADERS   Headers;
  ULONGLONG              BytesReceived;
  USHORT                 EntityChunkCount;
  PHTTP_DATA_CHUNK       pEntityChunks;
  HTTP_RAW_CONNECTION_ID RawConnectionId;
  PHTTP_SSL_INFO         pSslInfo;
}HTTP_REQUEST_V1, *PHTTP_REQUEST_V1;

I have only just started C# so delving into ??COM?? programming is over my head.

AND looking through the ducumentation, I cant see an 'entry' (by which I mean a simple send bytes-> receieve HTTP_REQUEST).

Anyhoo! if anyone wants to point me in the direction of some nice WINDOWS KERNEL MODE HTTP SERVERS INCLUDING SSL then feel free it would be a great read and something to consider for the future.

+3  A: 

You can't cast it to HttpWebRequest or anything like that. Just throw the Socket away and use HttpWebRequest instead. Otherwise, you'll have to manually parse the response byte[].

Mehrdad Afshari
+6  A: 

Have you considered using the HttpListener class instead of a socket to receive your incoming HTTP requests? It will yield HttpListenerRequest objects instead of raw data. I've found these classes useful for simulating a web server.

Chris W. Rea
+14  A: 

Hi, just replace Socket by using HttpListener. It parses the HTTP request for you, easily.

Here is an example:

HttpListener listener = new HttpListener(); 
// prefix URL at which the listener will listen
listener.Prefixes.Add("http://localhost:8080/");
listener.Start();
Console.WriteLine("Listening...");
while (true)
{
    // the GetContext method blocks while waiting for a request.
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;

    // process the request
    // if you want to process request from multiple clients 
    // concurrently, use ThreadPool to run code following from here
    Console.WriteLine("Client IP " + request.UserHostAddress);

    // in request.InputStream you have the data client sent
    // use context.Response to respond to client
}
Martin Konicek
not the answer :/
divinci