如何解决如何使用 Indy 处理 XMLHttpRequest POST 请求?
我无法使用 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 个字节。如果我使用 TMemoryStream
或 String
来获取响应没有区别。
这是我的代码:
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
解决方法
问题是 HTTPAnalyzer 向 TIdHTTP 显示不同的响应标头,
在我用 TIdHTTP.Response.RawHeaders
检查 ResponseHeaders 后,我得到了响应 Content-Encoding: gzip
,没有显示 HTTPAnalyzer,然后我添加了 Compressor
idHttpA.Compressor := TIdCompressorZLib.Create(idHttpA)
并且代码运行良好。
谢谢@RemyLebeau。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。