所以我一直在学习如何接收数据,同时使用GET向服务器传递更多数据的参数,但无法弄清楚为什么它不能为朋友工作(它对我来说非常好)。
function GetRequest(Host, Script, Request: string): string;
var
sock: TSocket;
RQAddr: TSockAddrIn;
WSA: TWSADATA;
ReceiveBuffer: array [0 .. 4096] of Char;
SendBuf: string;
len, i, npos: dword;
SentBytes, ReceivedBytes, RecvLen: Integer;
begin
RecvLen := 0;
Result := '';
SentBytes := 0;
WSAStartup($202, WSA);
sock := Socket(AF_INET, SOCK_STREAM, 6);
RQAddr.sin_family := AF_INET;
RQAddr.sin_port := htons(80);
RQAddr.sin_addr.S_addr := inet_addr(PChar(GetIPfromDNS(PChar(Host))));
if Connect(sock, RQAddr, sizeof(RQAddr)) = 0 then
begin
SendBuf := 'GET ' + Script + '?' + Request + ' HTTP/1.1'#13#10 +
'Host: ' + Host + #13#10 + 'User-Agent: NIL'#13#10 +
'Connection: close'#13#10#13#10; // 'Connection: Keep-Alive'#13#10#13#10;
len := Length(SendBuf);
repeat
SentBytes := SentBytes + Send(sock, SendBuf[1 + SentBytes],
len - SentBytes, 0);
until SentBytes >= len;
ZeroMemory(@ReceiveBuffer, sizeof(ReceiveBuffer));
ReceivedBytes := Recv(sock, ReceiveBuffer, 4097, 0);
if ReceivedBytes > 0 then
for i := 0 to ReceivedBytes - 1 do
if ReceiveBuffer[i] = #0 then
break
else
Result := Result + ReceiveBuffer[i];
{
nfil:Textfile;
AssignFile(nfil,'Get['+IntToStr(GetTickCount)+'].txt');
Rewrite(nfil);
Write(nfil,Result);
CloseFile(nfil); }
npos := pos(#13#10#13#10, Result);
if npos > 0 then
begin
inc(npos, 4);
Result := copy(Result, npos, Length(Result) - npos + 1);
end
else
Result := '';
closesocket(sock);
end;
WSACleanup;
end;
<?php
if (isset($_GET['test1']))
echo 'test1';
if (isset($_GET['test2']))
echo 'test2';
?>
我叫它:
result:=GetRequest('xxx.xxx','/xxx.php','test=1&test2=2');
结果很完美,我得到了我应该收到的数据。
我记录了他从服务器收到的数据并返回:
HTTP/1.1 200 OK
Server: hosting
Date: Fri, 26 Apr 2013 16:31:24 GMT
Content-Type: text/html
Content-Length: 0
Connection: close
X-Powered-By: PHP/5.3.16
我不知道为什么它不起作用所以我决定在这里问 希望我写得这么好就能得到一个答案(我觉得我应该更详细但不知道是什么:/,如果你发现任何错误我会想一想,我会尝试提供更多细节)。