2009-06-09 20 views
5

Tengo un socket que está recibiendo solicitudes HTTP.Cómo crear una clase .NET HttpWebRequest desde el byte recibido de un socket [] 's

Tengo una solicitud HTTP sin procesar en formato byte [] desde mi socket.

tengo que estudiar esta solicitud - PERO

vez de reinventar la rueda - puedo 'fundido' esta matriz de bytes en un System.NET.HttpWebRequest o algo similar?

ACTUALIZACIÓN ----- ---------

Así que de todos modos no pude encontrar la respuesta. Cavando un poco más, aunque creo que se podría hacer mediante llamadas a funciones en:

Httpapi.dll creo que el HttpWebRequest utiliza este DLL (WinXPSP2)

la estructura interesante es el 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; 

Acabo de comenzar C# tan ahondar en ?? COM ?? la programación está por encima de mi cabeza.

Y mirando a través de la ducumentación, no puedo ver una 'entrada' (me refiero a un simple enviar bytes-> recibir HTTP_REQUEST).

Anyhoo! si alguien quiere indicarme algunos WINDOWS KERNEL MODE HTTP SERVERS INCLUYENDO SSL, entonces siéntase libre sería una gran lectura y algo a considerar para el futuro.

+0

entiendo que no puedo 'echo' en el sentido tradicional/técnico, sino que refleja la clase HttpWebRequest y tener un golpe parece que el HttpWebRequest se analiza/creado a partir de la SerializationInfo de una WebRequest? también está sobre mi cabeza:/ – divinci

+0

Preguntas similares - http://stackoverflow.com/questions/318506/converting-raw-http-request-into-httpwebrequest-object - http://stackoverflow.com/questions/743794/net -http-parser –

Respuesta

15

simplemente reemplace Socket usando HttpListener. Analiza la solicitud HTTP por usted, fácilmente.

Aquí se muestra un ejemplo:

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 
} 
+0

no es la respuesta: / – divinci

3

No puede convertirlo a HttpWebRequest ni nada de eso. Solo tira el Socket y en su lugar usa HttpWebRequest. De lo contrario, tendrá que analizar manualmente la respuesta byte[].

6

¿Ha considerado utilizar la clase HttpListener en lugar de un socket para recibir sus solicitudes HTTP entrantes? Producirá HttpListenerRequest objetos en lugar de datos sin procesar. Encontré estas clases útiles para simular un servidor web.

Cuestiones relacionadas