C# HTTP 请求解析器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/975308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:39:15  来源:igfitidea点击:

C# HTTP Request Parser

c#.nethttp

提问by Gordon Thompson

Possible Duplicate:
Converting Raw HTTP Request into HTTPWebRequest Object

可能的重复:
将原始 HTTP 请求转换为 HTTPWebRequest 对象

I've got a custom HTTP server written in C# which gives me the raw HTTP request...

我有一个用 C# 编写的自定义 HTTP 服务器,它给了我原始的 HTTP 请求......

GET /ACTION=TEST HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

Is there something in the .NET framework that I can use to parse it or do I have to do it by hand?

.NET 框架中有什么东西可以用来解析它还是我必须手动解析它?

Cheers

干杯

采纳答案by NicolasP

Looks like this question has been asked before here. Apparently there is no built-in way to do it.

看起来这个问题已经在这里问过。显然没有内置的方法可以做到这一点。

回答by Charles

Check out HttpMachine- a component of the KayakHTTP server for dotNET. HttpMachine is a callback driven HTTP parser.

查看HttpMachine-用于 dotNET的KayakHTTP 服务器的组件。HttpMachine 是一个回调驱动的 HTTP 解析器。

To wet your appetite, here's the IHttpParserHandlerinterface:

为了满足你的胃口,这里是IHttpParserHandler接口:

using System
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HttpMachine
{
    public interface IHttpParserHandler
    {
        void OnMessageBegin(HttpParser parser);
        void OnMethod(HttpParser parser, string method);
        void OnRequestUri(HttpParser parser, string requestUri);
        void OnFragment(HttpParser parser, string fragment);
        void OnQueryString(HttpParser parser, string queryString);
        void OnHeaderName(HttpParser parser, string name);
        void OnHeaderValue(HttpParser parser, string value);
        void OnHeadersEnd(HttpParser parser);
        void OnBody(HttpParser parser, ArraySegment<byte> data);
        void OnMessageEnd(HttpParser parser);
    }
}