gpt4 book ai didi

德尔福+突触: How to check the size of the response with TTCPBlockSocket

转载 作者:行者123 更新时间:2023-12-03 15:55:09 26 4
gpt4 key购买 nike

我终于让我的 Delphi 应用程序能够通过 HTTPS 使用 Synapse 库的直接套接字发送数据。

但是,我无法确定返回给我的数据的大小。

目前,我有以下代码:

Socket     := TTCPBlockSocket.Create;
status := TStringList.Create;

status.Append('LineBuffer length: ' + IntToStr(Length(Socket.LineBuffer)));
status.Append('Waiting bytes: ' + IntToStr(Socket.WaitingDataEx));
status.Append('recvBufferStr: ' + CRLF + Socket.RecvBufferStr(240, 25000) );
status.Append('LastError = ' + Socket.LastErrorDesc);
status.Append('Error code: ' + IntToStr(Socket.LastError));
Memo1.Lines.AddStrings(status);

我在 Memo1 中得到以下内容:

socket lastError = 0
LineBuffer length: 0
Waiting bytes: 0
recvBufferStr:
HTTP/1.1 200 OK
Date: Thu, 22 Dec 2011 01:06:07 GMT
Server: Apache
Content-Length: 237
Connection: close
Content-Type: text/plain; charset=utf-8

[***The returned Data***]
<小时/>

如果Socket.RecvBufferStr的第一个参数(要接收的长度)太大,我收到winsock错误10054,因为即使服务器已完成发送数据,我仍在等待数据。

如果它太短(几乎总是如此),那么我只能获得指定数量的数据。

等待字节和行缓冲区长度显示 0(不确定这是否是因为它们是 longInt 并且我正在执行 IntToStr),所以我不认为这就是我检查数据量的方式。我也尝试过使用 CanRead和 CanReadEx 都无济于事。

我想做如下的事情:Socket.RecvBufferStr ([接收长度]、[直到接收到所有数据] 或 25000)。

如果还有其他功能,那很好,但我想坚持使用 TTCPBlockSocket因为我从 synapse 尝试过的 HTTPSend 和其他方法不能满足我的目的。

如何检查 - 使用 TTCPBlockSocket来自 Synapse 的套接字Delphi/Pascal 库 - 服务器返回给我多少数据?

最佳答案

尝试以下代码。它展示了如何从响应 header 获取 Content-Length 并读取内容本身。

请注意,HTTP 协议(protocol)远没有那么容易,但如果您要与自己的服务器或您知道其工作原理的服务器进行通信,您可能会将此作为灵感。并且不要忘记包含 OpenSSL使用此代码时的二进制文件(版本 0.9.8 肯定与 Synapse 兼容)。

uses
blcksock, ssl_openssl;

function HTTPGetText(const Host: string): AnsiString;
var
ContentLength: Integer;
HTTPHeader: AnsiString;
TCPSocket: TTCPBlockSocket;
begin
Result := '';

TCPSocket := TTCPBlockSocket.Create;
try
// initialize TTCPBlockSocket
TCPSocket.ConvertLineEnd := True;
TCPSocket.SizeRecvBuffer := c64k;
TCPSocket.SizeSendBuffer := c64k;

// connect to the host, port 443 is default for HTTP over SSL
TCPSocket.Connect(Host, '443');
// check for socket errors
if TCPSocket.LastError <> 0 then
Exit;

// server name identification
TCPSocket.SSL.SNIHost := Host;
// initialize and connect over OpenSSL
TCPSocket.SSLDoConnect;
// server name identification
TCPSocket.SSL.SNIHost := '';
// check for socket errors
if TCPSocket.LastError <> 0 then
Exit;

// build the HTTP request header
HTTPHeader :=
'GET / HTTP/1.0' + CRLF +
'Host: ' + Host + ':443' + CRLF +
'Keep-Alive: 300' + CRLF +
'Connection: keep-alive' + CRLF +
'User-Agent: Mozilla/4.0' + CRLF + CRLF;

// send the HTTP request
TCPSocket.SendString(HTTPHeader);
// check for socket errors
if TCPSocket.LastError <> 0 then
Exit;

// read the response status, here some servers might give you the content
// note, that we are waiting in a loop until the timeout or another error
// occurs; if we get some terminated line, we break the loop and continue
// to check if that line is the HTTP status code
repeat
HTTPHeader := TCPSocket.RecvString(5000);
if HTTPHeader <> '' then
Break;
until
TCPSocket.LastError <> 0;

// if the line we've received is the status code, like 'HTTP/1.1 200 OK'
// we will set the default value for content length to be read
if Pos('HTTP/', HTTPHeader) = 1 then
begin
ContentLength := -1;

// read the response header lines and if we find the 'Content-Length' we
// will store it for further content reading; note, that again, we are
// waiting in a loop until the timeout or another error occurs; if the
// empty string is received, it means we've read the whole response header
repeat
HTTPHeader := TCPSocket.RecvString(5000);
if Pos('Content-Length:', HTTPHeader) = 1 then
ContentLength := StrToIntDef(Trim(Copy(HTTPHeader, 16, MaxInt)), -1);
if HTTPHeader = '' then
Break;
until
TCPSocket.LastError <> 0;

// and now let's get the content, we know it's size, so we just call the
// mentioned RecvBufferStr function
if ContentLength <> -1 then
Result := TCPSocket.RecvBufferStr(ContentLength, 5000);
end;

TCPSocket.CloseSocket;

finally
TCPSocket.Free;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Memo1.Text := HTTPGetText('www.meebo.com');
end;

关于德尔福+突触: How to check the size of the response with TTCPBlockSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com