delphi HttpQueryInfo查询响应头信息,执行成功,但是返回的是空字符串
 procedure Get(url: string;res: TStream);
var
  hInt,hUrl:HINTERNET;
  buffer,hBuf:PChar;
  dwRead:cardinal;
  flag:Boolean;
  Len,index:Cardinal;
  stream:TStringStream;
  var value: DWORD;
begin
 GetMem(buffer, 65536);
 GetMem(hBuf, 65536);
 hInt := InternetOpen('Delphi',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
 dwRead:=0;
 hurl:=InternetOpenUrl(hInt,PChar(url),nil,0,INTERNET_FLAG_RELOAD,0);

 //查询响应头信息
 flag:=HttpQueryInfo(hurl,HTTP_QUERY_RAW_HEADERS_CRLF,hBuf,len,index);
 stream:=TStringStream.Create('');
 stream.Write(hBuf^,len);
 ShowMessage(stream.DataString);

 repeat
   InternetReadFile(hUrl,buffer,1000,dwRead);
   if dwRead<>0 then
     res.Write(buffer^, dwRead);
 until dwRead=0;

 InternetCloseHandle(hUrl);
 InternetCloseHandle(hInt);
 FreeMem(buffer);
end;

rt
delphi HttpQueryInfo查询响应头信息,执行成功,返回true,len也不为0,但是我从缓冲区读出来的字符串却是空的,不知道是我读取缓冲区的方式不对还是怎么的,有谁搞过,请多多指教!

1个回答

procedure Get(const url: string; head, res: TStream);
var
hInt, hUrl: HINTERNET;
buffer: PChar;
dwRead: cardinal;
flag: Boolean;
len, index: Cardinal;
begin
GetMem(buffer, 65536);
hInt := InternetOpen('Delphi', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hurl := InternetOpenUrl(hInt, PChar(url), nil, 0, INTERNET_FLAG_RELOAD, 0);
len := 65536;
index := 0;
//查询响应头信息
flag := HttpQueryInfo(hUrl, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, len, index);
if flag then
begin
head.Write(buffer^, len);
repeat
if not InternetReadFile(hUrl, buffer, 1000, dwRead) then
Break;
if dwRead <> 0 then
res.Write(buffer^, dwRead);
until dwRead = 0;
end;
InternetCloseHandle(hUrl);
InternetCloseHandle(hInt);
FreeMem(buffer);
end;

procedure TForm1.btn1Click(Sender: TObject);
var
head, res: TStringStream;
begin
res := TStringStream.Create('');
head := TStringStream.Create('');
try
Get('http://www.ip138.com', head, res);
ShowMessage(head.DataString);
ShowMessage(res.DataString);
finally
head.Free;
res.Free;
end;
end;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问