C# 使用HttpListener获取.net中传入数据的格式

C# 使用HttpListener获取.net中传入数据的格式,c#,.net,http,httprequest,C#,.net,Http,Httprequest,我正在写一个服务器端程序。 我创建了一个HttpListener来监听传入的请求。 我怎样才能知道发送的是哪种数据?例如,它是文本、图像、pdf、word吗 请更正我下面的代码,如果它是错误的。 我对这个很陌生,我对HTTP概念的理解可能是错误的。谢谢 main() { HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://192.168.1.2/"); listener.Start(); while

我正在写一个服务器端程序。 我创建了一个
HttpListener
来监听传入的请求。 我怎样才能知道发送的是哪种数据?例如,它是文本、图像、pdf、word吗

请更正我下面的代码,如果它是错误的。 我对这个很陌生,我对HTTP概念的理解可能是错误的。谢谢

main()
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://192.168.1.2/");
listener.Start();

while (true) //Keep on listening
{
context = listener.GetContext();
HttpListenerRequest request = context.Request;

//Do I get the request stream here, and do something with the stream to find out what data format is being sent?
Stream requestStream = request.InputStream;
}

}

了解发送的数据类型的唯一简单方法是查看请求的
Content-type
头(通过
ContentType
属性公开),该头应包含内容的MIME类型:

switch(request.ContentType)
{
    case "image/png":
    case "image/jpeg":
    case "image/bmp":
    case "image/gif":
    case "image/tiff":
        // OK, this is an image
        ...
        break;
    default:
        // Something else
        ...
        break;
}

请注意,这种方法并不总是有效的,因为客户端可以发送请求而不指定
内容类型
标题,或者发送与标题不匹配的数据

了解发送的数据类型的唯一简单方法是查看请求的
内容类型
头(通过
内容类型
属性公开),该头应包含内容的MIME类型:

switch(request.ContentType)
{
    case "image/png":
    case "image/jpeg":
    case "image/bmp":
    case "image/gif":
    case "image/tiff":
        // OK, this is an image
        ...
        break;
    default:
        // Something else
        ...
        break;
}

请注意,这种方法并不总是有效的,因为客户端可以发送请求而不指定
内容类型
标题,或者发送与标题不匹配的数据

也许可以从描述您试图实现的目标开始。实际上,我正在等待客户端通过Http发布一些图像数据。我只想监听图像数据,其他任何东西(没有pdf等)都不可能从描述您试图实现的目标开始。我实际上在等待客户端通过Http发布一些图像数据。我只想听听图像数据,别的什么都不想(没有pdf等)