枚举TWebRequest HTTP头域 [英] Enumerate TWebRequest HTTP header fields

查看:236
本文介绍了枚举TWebRequest HTTP头域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以转储TWebRequest(和TWebResponse)对象的所有头字段?
此刻,我使用GetFieldByName()并使用Writeln()打印它们,但只有当我已经知道标题字段的名称时才有效。我正在寻找一种获取所有标题字段名称以枚举每个字段的方法,但是我没有找到任何方法。



我写了一个REST数据点控制台应用程序,并希望将所有HTTP请求/响应记录到控制台。

解决方案

AFAIK不可能(Delphi XE2) p>

我已经使用了一些小的访问权限来访问原始头文件。
但是,这真的很脏!使用您自己的风险!



实际的类请求类是TIdHTTPAppRequest(警告:对于不同类型的webbroker应用程序可能会有所不同,我没有测试此代码使用不同种类的datasnap应用程序)。



所以诀窍是:



声明类助手类似于以下:

  TIdHTTPAppRequestHelper = TIdHTTPAppRequest的类助手
public
函数GetRequestInfo:TIdEntityHeaderInfo;
结束

实现

函数TIdHTTPAppRequestHelper.GetRequestInfo:TIdEntityHeaderInfo;
begin
结果:= FRequestInfo;
结束

以这种方式你可以使用这个帮助器来保护受保护的FRequestInfo域。



在OnAction事件处理程序中,您可以使用以下代码来获取所有标题名称:

  procedure Twm.wmWebActionItem1Action(Sender:TObject; Request:TWebRequest; 
响应:TWebResponse; var Handled:Boolean);
var
HeadersCount:Integer;
I:整数;
sw:TStreamWriter;
begin
Response.ContentType:='text / plain';
Response.ContentStream:= TMemoryStream.Create;
sw:= TStreamWriter.Create(Response.ContentStream);
try
HeadersCount:= TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.Count;
为I:= 0 to HeadersCount - 1 do
sw.WriteLine(TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.Names [I]);
finally
sw.Free;
结束
处理:= True;
结束

但是,TWebRequest不允许读取原始标题是不好的。应该改变!


is it possible to dump all the header fields of a TWebRequest (and TWebResponse) object? At the moment I use GetFieldByName() and print them with Writeln() but this works only if I already know the name of the header field. I'm looking for a way to obtain all header field names to enumarate each field but I didn't find any method to do that.

I wrote a REST datasnap console application and wants to log all HTTP requests/responses to console.

解决方案

AFAIK it is not possibile (Delphi XE2).

I've used a little trink to have access to the raw headers. However, this is really dirty! Use at you own risk!

The actual class request class is the TIdHTTPAppRequest (WARNING: Could be different for different type of webbroker app. I''ve not tested this code with different kind of datasnap app).

So the trick is:

Declare a class helper similar to the following:

  TIdHTTPAppRequestHelper = class helper for TIdHTTPAppRequest
  public
    function GetRequestInfo: TIdEntityHeaderInfo;
  end;

  implementation

  function TIdHTTPAppRequestHelper.GetRequestInfo: TIdEntityHeaderInfo;
  begin
    Result := FRequestInfo;
  end;

In this way you can use this helper to have acccess to the protected FRequestInfo field.

In the OnAction event handler you can use the following code to have all the headers names:

procedure Twm.wmWebActionItem1Action(Sender: TObject; Request: TWebRequest;
                 Response: TWebResponse; var Handled: Boolean);
var
  HeadersCount: Integer;
  I: Integer;
  sw: TStreamWriter;
begin
  Response.ContentType := 'text/plain';
  Response.ContentStream := TMemoryStream.Create;
  sw := TStreamWriter.Create(Response.ContentStream);
  try
    HeadersCount := TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.Count;
    for I := 0 to HeadersCount - 1 do
      sw.WriteLine(TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.Names[I]);
  finally
    sw.Free;
  end;
  Handled := True;
end;

However, it is bad that TWebRequest do not allows to read the raw headers. That should be changed!

这篇关于枚举TWebRequest HTTP头域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆