将原始HTTP请求转换为HTTPWebRequest对象

dr.*_*vil 23 .net tcp http httpwebrequest

在.NET中,是否可以将原始HTTP请求转换为HTTPWebRequest对象?

我确信.NET在内部做到了.知道.NET的哪个部分实际上正在处理这个问题?我可以调用它还是有任何允许原始HTTP连接的外部库?

Rya*_*ook 9

我不相信有一种暴露的方法来做到这一点.您可能必须找到或编写解析器来中断请求,然后编写自己的扩展HttpWebRequest的类.

以下是CodeProject的解析器:

http://www.codeproject.com/KB/IP/CSHTTPServer.aspx

我查看了HttpWebRequest的转子代码(简要地说),我没有看到任何突出的银弹.这是文件的链接:

http://www.123aspx.com/Rotor/RotorSrc.aspx?rot=40844

所有转子代码都在这里用于在线浏览:

http://www.123aspx.com/Rotor/default.aspx

在这里你可以下载它:

http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en

我知道一堆链接并没有真正回答你的问题,但我不认为你正在寻找的功能是在框架中公开的.我希望被证明是错误的,所以如果你找到了一个很好的方法,请更新帖子.我知道那里的工具必须这样做,任何用.Net写的记录原始请求的东西,然后让你重新提交它们就是做类似的事情.我相信fiddler(http://www.fiddler2.com)是用.Net编写的,你可能想给这些人发一封电子邮件,看看他们是否可以提供帮助.

  • 除了现在 123aspx.com 链接已损坏。人们,不要只是发布链接并提供真实的答案!“始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线” http://stackoverflow.com/help/how-to-answer (2认同)

dim*_*aan 5

现在可能,但仅限于 .Net Core 2.0+。使用Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser类:

using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Text;

public class Program : IHttpRequestLineHandler, IHttpHeadersHandler
{
    public static void Main(string[] args)
    {
        string requestString =
@"POST /resource/?query_id=0 HTTP/1.1
Host: example.com
User-Agent: custom
Accept: */*
Connection: close
Content-Length: 20
Content-Type: application/json

{""key1"":1, ""key2"":2}";
        byte[] requestRaw = Encoding.UTF8.GetBytes(requestString);
        ReadOnlySequence<byte> buffer = new ReadOnlySequence<byte>(requestRaw);
        HttpParser<Program> parser = new HttpParser<Program>();
        Program app = new Program();
        Console.WriteLine("Start line:");
        parser.ParseRequestLine(app, buffer, out var consumed, out var examined);
        buffer = buffer.Slice(consumed);
        Console.WriteLine("Headers:");
        parser.ParseHeaders(app, buffer, out consumed, out examined, out var b);
        buffer = buffer.Slice(consumed);
        string body = Encoding.UTF8.GetString(buffer.ToArray());
        Dictionary<string, int> bodyObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, int>>(body);
        Console.WriteLine("Body:");
        foreach (var item in bodyObject)
            Console.WriteLine($"key: {item.Key}, value: {item.Value}");
        Console.ReadKey();
    }

    public void OnHeader(Span<byte> name, Span<byte> value)
    {
        Console.WriteLine($"{Encoding.UTF8.GetString(name)}: {Encoding.UTF8.GetString(value)}");
    }

    public void OnStartLine(HttpMethod method, HttpVersion version, Span<byte> target, Span<byte> path, Span<byte> query, Span<byte> customMethod, bool pathEncoded)
    {
        Console.WriteLine($"method: {method}");
        Console.WriteLine($"version: {version}");
        Console.WriteLine($"target: {Encoding.UTF8.GetString(target)}");
        Console.WriteLine($"path: {Encoding.UTF8.GetString(path)}");
        Console.WriteLine($"query: {Encoding.UTF8.GetString(query)}");
        Console.WriteLine($"customMethod: {Encoding.UTF8.GetString(customMethod)}");
        Console.WriteLine($"pathEncoded: {pathEncoded}");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Text;

public class Program : IHttpRequestLineHandler, IHttpHeadersHandler
{
    public static void Main(string[] args)
    {
        string requestString =
@"POST /resource/?query_id=0 HTTP/1.1
Host: example.com
User-Agent: custom
Accept: */*
Connection: close
Content-Length: 20
Content-Type: application/json

{""key1"":1, ""key2"":2}";
        byte[] requestRaw = Encoding.UTF8.GetBytes(requestString);
        ReadOnlySequence<byte> buffer = new ReadOnlySequence<byte>(requestRaw);
        HttpParser<Program> parser = new HttpParser<Program>();
        Program app = new Program();
        Console.WriteLine("Start line:");
        parser.ParseRequestLine(app, buffer, out var consumed, out var examined);
        buffer = buffer.Slice(consumed);
        Console.WriteLine("Headers:");
        parser.ParseHeaders(app, buffer, out consumed, out examined, out var b);
        buffer = buffer.Slice(consumed);
        string body = Encoding.UTF8.GetString(buffer.ToArray());
        Dictionary<string, int> bodyObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, int>>(body);
        Console.WriteLine("Body:");
        foreach (var item in bodyObject)
            Console.WriteLine($"key: {item.Key}, value: {item.Value}");
        Console.ReadKey();
    }

    public void OnHeader(Span<byte> name, Span<byte> value)
    {
        Console.WriteLine($"{Encoding.UTF8.GetString(name)}: {Encoding.UTF8.GetString(value)}");
    }

    public void OnStartLine(HttpMethod method, HttpVersion version, Span<byte> target, Span<byte> path, Span<byte> query, Span<byte> customMethod, bool pathEncoded)
    {
        Console.WriteLine($"method: {method}");
        Console.WriteLine($"version: {version}");
        Console.WriteLine($"target: {Encoding.UTF8.GetString(target)}");
        Console.WriteLine($"path: {Encoding.UTF8.GetString(path)}");
        Console.WriteLine($"query: {Encoding.UTF8.GetString(query)}");
        Console.WriteLine($"customMethod: {Encoding.UTF8.GetString(customMethod)}");
        Console.WriteLine($"pathEncoded: {pathEncoded}");
    }
}
Run Code Online (Sandbox Code Playgroud)