.NET HTTP 处理程序——如何发送自定义响应?
我已将自定义(基于套接字)服务器转换为 IIS (.NET 4.0) 中的 HTTP 处理程序。
我已将客户端转换为以正确的格式发送 HTTP GET。
我不想更改客户端的响应处理代码来解析 HTML 代码。这是一种不必要的后背疼痛。我不需要此服务器与浏览器一起使用或与除我的客户端之外的任何东西兼容。
所以,我基本上需要 HTTP 处理程序(服务器)来发送客户端期望的响应 - 没有“HTML/1.0”、标头等。
我目前正在使用 HttpContext.Response.Write() 方法。我认为有一种方法可以将原始数据写入响应,或者有一种方法可以删除我想要发送的数据周围的所有标头和 HTML 标记。
预先感谢您的任何帮助。 戴夫
I have converted my custom (socket-based) server over to an HTTP Handler in IIS (.NET 4.0).
I have converted my client to send an HTTP GET in the proper format.
I do not want to change my client's response-handling code to parse out the HTML code. That is a pain in the rear that is also unnecessary. I do not need this server to work with a browser or be compatible with anything except my client.
So, I basically need the HTTP Handler (server) to send the response that the client expects -- without the "HTML/1.0", headers and whatnot.
I am using the HttpContext.Response.Write() method at the moment. I figure there is either a way to write raw data to the response, or a way to delete all the headers and HTML tags around the data I want to send.
Thanks in advance for any help.
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在浏览器中查看此内容进行验证,那么您可能会看到浏览器尝试为您修复 HTML。
例如,如果我的处理程序中有这个:
那么响应标头将如下所示:
响应的实际内容很简单
您会注意到默认内容类型是 text/html,但是,您基本上可以完全控制响应。您可以清除标头、添加新标头并相应地设置内容类型:
这将产生以下响应标头:
这样就完成了。查看 HttpResponse 上的文档,以便您可以了解所有可用的内容,并且您应该能够从响应中准确地获取您想要的内容,而无需任何额外的麻烦,并且看不到任何 HTML。
If you are looking at this in a browser to verify, then what you may be seeing is the browsers attempt to fix your HTML for you.
For instance if I have this in my Handler:
Then the Response Headers will look like this:
The actual content of the response is simply
You'll notice the default content type is text/html, however, you basically have full control over the response. You can clear the headers, add new ones, and set the content type accordingly:
Which will yield the following Response Headers:
So there you have it. Take a look at the docs on HttpResponse so you can see all that is available to you, and you should be able to get exactly what you want out of the responses without any extra headache, and no HTML anywhere in sight.