使用 HttpListener 获取 .net 中传入数据的格式
我正在编写一个服务器端程序。 我创建了一个 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;
}
}
I am writing a server-side program.
I created a HttpListener
to listen for incoming requests.
How can I find out what kind of data is being sent in? E.g. is it a text, image, pdf, word?
Pls correct my code below if it is wrong.
I'm really new to this and my understanding of the HTTP concepts may be wrong. Thanks.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
了解正在发送的数据类型的唯一简单方法是查看请求的
Content-Type
标头(通过ContentType
属性公开),该标头应包含 MIME内容类型:请注意,这种方法并不总是有效,因为客户端可以在不指定
Content-Type
标头的情况下发送请求,或者发送与标头不匹配的数据...The only easy way to know what type of data is being sent is by looking at the request's
Content-Type
header (exposed via theContentType
property), which should contain the MIME type of the content:Note that this approach won't always work, because the client could send a request without specifying the
Content-Type
header, or send data that doesn't match the header...