How to add/overwrite a HTTP header using THTTPReqResp?

99封情书 提交于 2019-12-04 01:43: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;


来源:https://stackoverflow.com/questions/13345540/how-to-add-overwrite-a-http-header-using-thttpreqresp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!