如何从Socket的接收字节[]创建.NET HttpWebRequest类

div*_*nci 7 .net c# sockets httpwebrequest

我有一个接收HTTP请求的套接字.

所以我有一个来自socket的byte []形式的原始http请求.

我必须研究这个要求 - 但是

而不是重新发明轮子 - 我可以这个字节数组'转换成System.Net.HttpWebRequest或类似的东西吗?

-----更新---------

所以无论如何我找不到答案.通过进一步挖掘虽然我认为可以通过调用函数来完成:

HttpApi.dll 我认为HttpWebRequest使用这个dll(winxpsp2)

有趣的结构是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;
Run Code Online (Sandbox Code Playgroud)

我刚刚开始使用C#所以钻研?? COM?编程是我的头脑.

通过讨论,我看不到'条目'(我的意思是简单的发送字节 - >接收HTTP_REQUEST).

安美居!如果有人想让我指向一些不错的WINDOWS KERNEL模式HTTP服务器,包括SSL,那么感觉自由它将是一个伟大的阅读和未来考虑的事情.

Mar*_*cek 15

只需使用HttpListener替换Socket .它可以轻松地为您解析HTTP请求.

这是一个例子:

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
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*Rea 6

您是否考虑过使用HttpListener类而不是套接字来接收传入的HTTP请求?它将产生HttpListenerRequest对象而不是原始数据.我发现这些类对于模拟Web服务器很有用.