How to add/overwrite a HTTP header using THTTPReqR

2019-02-18 22:02发布

问题:

I want to approach the Exchange EWS webservice and handle XML SOAP composition (request) and parsing (response) myself. Therefore, THTPPRIO seems a bit overkill.

I'm trying THTTPReqResp, but I'm stuck here:

The web service does not follow the specs and expects a

Content-Type: text/xml; charset=utf-8

instead of

Content-Type: text/xml; charset="utf-8"

How can I add/overwrite a header using THTTPReqResp? Here's the code so far:

HTTPReqResp1.SoapAction := '"http://schemas.microsoft.com/exchange/services/2006/messages/ResolveNames"';
// HTTPReqResp1.UseUTF8InHeader := true; // Already
HTTPReqResp1.URL := 'https://webmail.mailserver.nl/ews/exchange.asmx';
HTTPReqResp1.Execute(TSRequest,TSResponse);

The Content-Type error occurs on the Execute (or on the Receive if I use Send/Recieve instead of Execute)

BTW If THTTPReqResp is not the right way to, comments are welcome. I'm also trying TidHTTP, see this post.

Delphi XE2 Update 4 with Indy 10.5.8.0

Thanks Jan

回答1:

I found it:

procedure TForm1.BeforeRRPost(const HTTPReqResp: THTTPReqResp; Data: Pointer);
const
   cContentHeader = 'Content-Type: text/xml; charset=utf-8';
begin
   HttpAddRequestHeaders(Data, PChar(cContentHeader), Length(cContentHeader), HTTP_ADDREQ_FLAG_REPLACE);
// Or  HttpAddRequestHeaders(Data, PChar(cContentHeader), Length(cContentHeader), HTTP_ADDREQ_FLAG_ADD);
end;

and then before the HTTPReqResp1.Execute or HTTPReqResp1.Send:

HTTPReqResp1.OnBeforePost := BeforeRRPost;