.NET HTTP处理程序 - 如何发送自定义响应?
我已经转换我的自定义(基于套接字)服务器通过在IIS(.NET 4.0)的HTTP处理程序。
I have converted my custom (socket-based) server over to an HTTP Handler in IIS (.NET 4.0).
我已经将我的客户端发送一个HTTP GET以正确的格式。
I have converted my client to send an HTTP GET in the proper format.
我不想改变我的客户端的响应,处理code解析出HTML code。即在后方,这也是不必要的痛苦。我不需要这个服务器与浏览器的工作或与任何东西,除了我的客户端不兼容。
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.
所以,基本上,我需要的HTTP处理程序(服务器)来发送客户期望的响应 - 没有HTML / 1.0,页眉和诸如此类的东西
So, I basically need the HTTP Handler (server) to send the response that the client expects -- without the "HTML/1.0", headers and whatnot.
我使用的是HttpContext.Response.Write()方法的时刻。我想,有两种办法写原始数据的反应,还是有办法来删除身边我要发送的数据的所有头和HTML标记。
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
如果您正在寻找这个在浏览器中进行验证,那么你可能看到的是浏览器尝试的修订的你的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:
public void ProcessRequest(HttpContext context)
{
context.Response.Write("Hello World");
}
那么响应头看起来是这样的:
Then the Response Headers will look like this:
HTTP / 1.1 200 OK
服务器:ASP.NET开发服务器/ 10.0.0.0
日期:星期六,2011年9月3日3时26分36秒GMT
的X ASPNET-版本:4.0.30319
缓存控制:私人
内容类型:的text / html ;字符集= UTF-8
内容长度:11
连接:关闭
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 03 Sep 2011 03:26:36 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 11
Connection: Close
响应的实际内容仅仅是
您好世界
您会发现默认的内容类型的的text / html ,但是,你基本上可以完全控制的响应。您可以清除标题,添加新的,并设置相应的内容类型:
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:
public void ProcessRequest(HttpContext context)
{
context.Response.ClearHeaders();
context.Response.AddHeader("my-skillz", "being AWESOME!");
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
这将产生以下响应头:
Which will yield the following Response Headers:
HTTP / 1.1 200 OK
服务器:ASP.NET开发服务器/ 10.0.0.0
日期:星期六,2011年9月3日三时40分14秒GMT
的X ASPNET-版本:4.0.30319
我-的skillz:是真棒
缓存控制:私人
内容类型:文本/纯;字符集= UTF-8
内容长度:11
连接:关闭
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 03 Sep 2011 03:40:14 GMT
X-AspNet-Version: 4.0.30319
my-skillz: being AWESOME!
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 11
Connection: Close
所以你有它。看看在的Htt presponse 中的文档,所以你可以看到所有可用你,和你应该能够得到你想要什么了回应没有任何多余的头痛,并且看不到HTML的任何地方。
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.