.net 将原始 HTTP 请求转换为 HTTPWebRequest 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/318506/
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
Converting Raw HTTP Request into HTTPWebRequest Object
提问by dr. evil
In .NET is it possible to convert a raw HTTP request to HTTPWebRequest object?
在 .NET 中是否可以将原始 HTTP 请求转换为 HTTPWebRequest 对象?
I'm sure .NET internally doing it. Any idea which part of the .NET is actually handling this? Can I call it or is there any external library which allows raw HTTP connections?
我确定 .NET 在内部这样做。知道 .NET 的哪一部分实际上在处理这个问题吗?我可以调用它还是有任何允许原始 HTTP 连接的外部库?
回答by Ryan Cook
I dont believe there is an exposed method to do this. You may have to find or write a parser to break the request up and then write your own class that extends HttpWebRequest.
我不相信有一种公开的方法可以做到这一点。您可能必须找到或编写一个解析器来分解请求,然后编写您自己的扩展 HttpWebRequest 的类。
Here is what looks like a parser from CodeProject:
以下是 CodeProject 中的解析器:
http://www.codeproject.com/KB/IP/CSHTTPServer.aspx
http://www.codeproject.com/KB/IP/CSHTTPServer.aspx
I looked at the rotor code for the HttpWebRequest (briefly) and I did not see anything that stood out as a silver bullet. Here is the link to the file:
我查看了 HttpWebRequest 的转子代码(简要),我没有看到任何突出的银弹。这是文件的链接:
http://www.123aspx.com/Rotor/RotorSrc.aspx?rot=40844
http://www.123aspx.com/Rotor/RotorSrc.aspx?rot=40844
All of the rotor code is here for browsing online:
所有转子代码都在这里供在线浏览:
http://www.123aspx.com/Rotor/default.aspx
http://www.123aspx.com/Rotor/default.aspx
And here you can download it:
你可以在这里下载它:
I know a bunch of links doesn't really answer your question, but I don't think the functionality that you are looking for is exposed in the framework. I would love to be proven wrong, so please update the post if you find a good way of doing it. I know tools out there must do it, anything written in .Net that logs raw requests and then lets you resubmit them is doing something similar. I believe fiddler(http://www.fiddler2.com) is written in .Net, you may want to shoot an email over to those guys and see if they can help.
我知道一堆链接并不能真正回答您的问题,但我认为您正在寻找的功能并未在框架中公开。我很想被证明是错误的,所以如果你找到一个好的方法,请更新帖子。我知道那里的工具必须这样做,任何用 .Net 编写的记录原始请求然后让您重新提交它们的东西都在做类似的事情。我相信fiddler( http://www.fiddler2.com) 是用 .Net 编写的,您可能想向这些人发送电子邮件,看看他们是否可以提供帮助。
回答by dimaaan
Its possible now, but only with .Net Core 2.0+. Use Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParserclass:
现在可能,但仅限于 .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}");
}
}
Output:
输出:
Start line:
method: Post
version: Http11
target: /resource/?query_id=0
path: /resource/
query: ?query_id=0
customMethod:
pathEncoded: False
Headers:
Host: example.com
User-Agent: custom
Accept: */*
Connection: close
Content-Length: 20
Content-Type: application/json
Body:
key: key1, value: 1
key: key2, value: 2
回答by DougN
Google for Cassinni which was an HTTP server with source originally offered by Microsoft that could host ASP.NET calls. You do have to parse the request yourself and load it but Cassinni would be a good starting point. This URL might help:
Google for Cassinni 这是一个 HTTP 服务器,其源代码最初由 Microsoft 提供,可以托管 ASP.NET 调用。您必须自己解析请求并加载它,但 Cassinni 将是一个很好的起点。这个 URL 可能有帮助:
http://blogs.msdn.com/dmitryr/archive/2005/09/27/474534.aspx
http://blogs.msdn.com/dmitryr/archive/2005/09/27/474534.aspx

![.net 错误:[Microsoft][ODBC 驱动程序管理器] 未找到数据源名称且未指定默认驱动程序](/res/img/loading.gif)