如何使用 Indy 处理 XMLHttpRequest POST 请求?

时间:2021-06-21 01:59:35

标签: delphi xmlhttprequest indy

我无法使用 XMLHttpRequest 发送 TIdHTTP 请求。这是我的标题:

Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cache-control:no-cache
Connection:keep-alive
DNT:1
Faces-Request:partial/ajax
X-Requested-With:XMLHttpRequest
Accept:application/xml, text/xml, */*; q=0.01
Accept-Language:de-DE
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding:gzip, deflate
Content-Length:517

这是响应头:

(Status-Line):HTTP/1.1 200 OK
Date:Mon, 21 Jun 2021 01:02:28 GMT
Server:Apache
X-ISPI-PERF:[Apache D=28068us b=0%]
X-ISPI-REQ-ID:xxxxxxxxxxxxxxxxxxxx
X-UA-Compatible:IE=edge
Pragma:no-cache
Cache-Control:no-cache, no-store
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control:no-cache
Content-Type:text/xml;charset=UTF-8
X-Frame-Options:SAMEORIGIN
Set-Cookie:JSESSIONID=xxxx; Path=/air; Secure
Vary:Accept-Encoding,User-Agent
X-Content-Type-Options:no-sniff
Strict-Transport-Security:max-age=[; includeSubDomains]
X-XSS-Protection:1; mode=block
Keep-Alive:timeout=5, max=98
Connection:Keep-Alive
Content-Length:9642

如何处理每次响应以获取完整内容?问题是有时我只得到 2000 个字节而不是 9600 个字节。如果我使用 TMemoryStreamString 来获取响应没有区别。

这是我的代码:

idHttpA := TIdHTTP.Create(nil);
idHttpA.HTTPOptions := [hoTreat302Like303];
lIOHandlerA := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
lIOHandlerA.SSLOptions.Mode := sslmClient;
lIOHandlerA.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
//lIOHandlerA.SSLOptions.Method := sslvTLSv1_2; *must be deleted because of SSLVersions 
idHttpA.IOHandler := lIOHandlerA;  
....
try
  res := idHttpA.Post('https://server/Start.xhtml', List);
except
  on e:EIdSocketError do
    ShowMessage('EIdSocketError: ' + e.Message);
  on e:EIdReadTimeout do
    ShowMessage('EIdReadTimeout: ' + e.Message);
  on e:EIDHttpProtocolException do
    ShowMessage('EIDHttpProtocolException: ' + IntToStr(e.ErrorCode));
  on e:Exception do
    ShowMessage('Exception: ' + e.Message);
end;

以下是我通过 TIdHTTP.Response.RawHeaders 获得的响应标头:

Date: Mon, 21 Jun 2021 18:30:22 GMT
Server: Apache
X-ISPI-PERF: [Apache D=45046us b=2%]
X-ISPI-REQ-ID: xxxxxxxxxxxxxxxxxxx
X-UA-Compatible: IE=edge
Pragma: no-cache
Cache-Control: no-cache, no-store
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache
Content-Type: text/xml;charset=UTF-8
X-Frame-Options: SAMEORIGIN
Set-Cookie: JSESSIONID=xxxx.3; Path=/air; Secure
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
X-Content-Type-Options: no-sniff
Strict-Transport-Security: max-age=[; includeSubDomains]
X-XSS-Protection: 1; mode=block
Connection: close

1 个答案:

答案 0 :(得分:2)

问题是 HTTPAnalyzerTIdHTTP 显示不同的响应标头, 在我用 TIdHTTP.Response.RawHeaders 检查 ResponseHeaders 后,我得到了响应 Content-Encoding: gzip,没有显示 HTTPAnalyzer,然后我添加了 Compressor idHttpA.Compressor := TIdCompressorZLib.Create(idHttpA) 并且代码运行良好。

谢谢@RemyLebeau。

相关问题