gpt4 book ai didi

c# - (部分)C# HTTP 服务器无法识别 HTTP 请求

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:09 24 4
gpt4 key购买 nike

在我的 visual studio 13 控制台应用程序中,我在 TCP 套接字上接收到以下字节流(从嵌入式设备接收):

POST /setup HTTP/1.1
Content-Length: 6
Content-Type: application/setup+tlv8

TLV8-data

虽然它看起来是一个有效的 http 请求,但我的以下尝试都没有成功地将它识别为 http 请求:(在常规 HTTP 请求上它们工作得很好)

到目前为止,我实际上只对来自 POST 或 GET 的请求路径以及正文中附加的内容(tlv 格式)感兴趣。

我配置有误吗?例如:我需要告诉正确的内容类型?有什么方法可以摆脱编写自己的简单文本解析器的麻烦吗?

小道消息情况下的代码示例:

private void init()
{
s = new PairServer();
s.Host = "172.28.22.78";
s.Port = "52025";
s.Start();
}

提供以下服务器类:

public class PairServer : RestServer
{
[RestRoute(Method = HttpMethod.POST, PathInfo = @"^/setup")]
[RestRoute(Method = HttpMethod.GET, PathInfo = @"^/setup")]
public void PairSetup(HttpListenerContext context)
{
// will not reach here
}

[RestRoute(Method = HttpMethod.POST)]
public void AnyRoute(HttpListenerContext context)
{
// Not even here
}

最佳答案

Although it seem to be a valid http request

不,那不是有效的 HTTP 请求。有效的 HTTP 请求,如 specification声明,必须包含一个 Host 请求 header :

A client MUST include a Host header field in all HTTP/1.1 request messages . If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value. An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field.

因此请确保您的客户遵循规范:

POST /setup HTTP/1.1
Host: example.com
Content-Length: 6
Content-Type: application/setup+tlv8

TLV8-data

关于c# - (部分)C# HTTP 服务器无法识别 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26679581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com